mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fix has_many assocation w/select load after create
If you create a new record via a collection association proxy that has not loaded its target, and which selects additional attributes through the association, then when the proxy loads its target, it will inadvertently trigger an ActiveModel::MissingAttributeError during attribute writing when CollectionAssociation#merge_target_lists attempts to do its thing, since the newly loaded records will possess attributes the created record does not. This error also raises a bogus/confusing deprecation warning when accessing the association in Rails 3.2.x, so cherry-pick would be appreciated!
This commit is contained in:
parent
2b83b07307
commit
9f3b8cd5a5
2 changed files with 9 additions and 1 deletions
|
@ -414,7 +414,7 @@ module ActiveRecord
|
||||||
persisted.map! do |record|
|
persisted.map! do |record|
|
||||||
if mem_record = memory.delete(record)
|
if mem_record = memory.delete(record)
|
||||||
|
|
||||||
(record.attribute_names - mem_record.changes.keys).each do |name|
|
((record.attribute_names & mem_record.attribute_names) - mem_record.changes.keys).each do |name|
|
||||||
mem_record[name] = record[name]
|
mem_record[name] = record[name]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -231,6 +231,14 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase
|
||||||
assert_equal "2", categories(:sti_test).authors_with_select.first.post_id.to_s
|
assert_equal "2", categories(:sti_test).authors_with_select.first.post_id.to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_create_through_has_many_with_piggyback
|
||||||
|
category = categories(:sti_test)
|
||||||
|
ernie = category.authors_with_select.create(:name => 'Ernie')
|
||||||
|
assert_nothing_raised do
|
||||||
|
assert_equal ernie, category.authors_with_select.detect {|a| a.name == 'Ernie'}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_include_has_many_through
|
def test_include_has_many_through
|
||||||
posts = Post.all.merge!(:order => 'posts.id').to_a
|
posts = Post.all.merge!(:order => 'posts.id').to_a
|
||||||
posts_with_authors = Post.all.merge!(:includes => :authors, :order => 'posts.id').to_a
|
posts_with_authors = Post.all.merge!(:includes => :authors, :order => 'posts.id').to_a
|
||||||
|
|
Loading…
Reference in a new issue