Skip to content

Instantly share code, notes, and snippets.

@ChaelCodes
Created April 25, 2021 16:19
Show Gist options
  • Save ChaelCodes/3923597e7a72c294a145a182b4135fa4 to your computer and use it in GitHub Desktop.
Save ChaelCodes/3923597e7a72c294a145a182b4135fa4 to your computer and use it in GitHub Desktop.
Rails #41986 Reproduction
# frozen_string_literal: true
# In a many-to-many relationship, when setting values using
# ids, and nested_attributes, if the attributes come first,
# they are overriden by the ids. If the ids come first, the
# attributes are included too!
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# gem "rails", github: "rails/rails", branch: "main"
gem "rails", "6.1.3.1"
gem "sqlite3"
end
require "active_record"
require "minitest/autorun"
require "logger"
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
end
create_table :comments, force: true do |t|
t.integer :post_id
t.string :text
end
create_table :authors, force: true do |t|
t.string :name
end
create_join_table(:posts, :authors)
end
class Post < ActiveRecord::Base
has_many :comments
has_and_belongs_to_many :authors
accepts_nested_attributes_for :comments
accepts_nested_attributes_for :authors
end
class Author < ActiveRecord::Base
has_and_belongs_to_many :posts
accepts_nested_attributes_for :posts
end
class Comment < ActiveRecord::Base
belongs_to :post
end
class BugTest < Minitest::Test
def test_one_to_many_association
comment = Comment.create! text: 'Hello world'
post = Post.create!(comment_ids: [comment.id],
comments_attributes: [{ text: "Hello World is the 'first!1!' of the Programming World" }])
post.reload
assert_equal 1, Post.count
assert_equal 2, post.comments.count
assert_equal 2, Comment.count
assert_equal post.id, Comment.second.post.id
assert_equal post.id, Comment.first.post.id
end
def test_many_to_many_association_attributes_first
author = Author.create! name: 'ChaelCodes'
post = Post.create!(authors_attributes: [{ name: 'Twitch Chat' }], author_ids: [author.id])
post.reload
assert_equal 1, Post.count
assert_equal 2, Author.count
assert_equal 2, post.authors.count
assert_includes Author.second.post_ids, post.id
assert_includes Author.first.post_ids, post.id
end
def test_many_to_many_association_ids_first
author = Author.create! name: 'ChaelCodes'
post = Post.create!(author_ids: [author.id], authors_attributes: [{ name: 'Twitch Chat' }])
post.reload
assert_equal 1, Post.count
assert_equal 2, Author.count
assert_equal 2, post.authors.count
assert_includes Author.second.post_ids, post.id
assert_includes Author.first.post_ids, post.id
end
# It's a pun - Post Test, and post-test, get it?
def post_test_teardown
Post.destroy_all
Author.destroy_all
Comment.destroy_all
end
alias teardown post_test_teardown
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment