diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2017-06-30 02:29:52 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2017-06-30 02:29:52 +0900 |
commit | 131ed24f5ef41f5470e83fb66cfff61f635bf24d (patch) | |
tree | 12b636efd8c477cb516a164b9d82c8f59d4972b3 /activerecord | |
parent | a317af93a39edcd3adbdea21e7d5c377709968f4 (diff) | |
download | rails-131ed24f5ef41f5470e83fb66cfff61f635bf24d.tar.gz rails-131ed24f5ef41f5470e83fb66cfff61f635bf24d.tar.bz2 rails-131ed24f5ef41f5470e83fb66cfff61f635bf24d.zip |
Should be clear `@association_ids` when joined newly associated record
Fixes #29627.
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/associations/collection_association.rb | 13 | ||||
-rw-r--r-- | activerecord/test/cases/associations/has_many_associations_test.rb | 5 |
2 files changed, 16 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index bbf3dbb75e..a49fb155ee 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -66,6 +66,7 @@ module ActiveRecord def reset super @target = [] + @association_ids = nil end def find(*args) @@ -355,7 +356,10 @@ module ActiveRecord transaction do add_to_target(build_record(attributes)) do |record| yield(record) if block_given? - insert_record(record, true, raise) { @_was_loaded = loaded? } + insert_record(record, true, raise) { + @_was_loaded = loaded? + @association_ids = nil + } end end end @@ -428,7 +432,12 @@ module ActiveRecord records.each do |record| raise_on_type_mismatch!(record) add_to_target(record) do - result &&= insert_record(record, true, raise) { @_was_loaded = loaded? } unless owner.new_record? + unless owner.new_record? + result &&= insert_record(record, true, raise) { + @_was_loaded = loaded? + @association_ids = nil + } + end end end diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index a936017ae3..d7515a4c18 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -2544,6 +2544,11 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal [bulb.id], car.bulb_ids assert_no_queries { car.bulb_ids } + + bulb2 = car.bulbs.create! + + assert_equal [bulb.id, bulb2.id], car.bulb_ids + assert_no_queries { car.bulb_ids } end def test_loading_association_in_validate_callback_doesnt_affect_persistence |