aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorBogdan Gusiev <agresso@gmail.com>2019-02-06 16:28:48 +0200
committerBogdan Gusiev <agresso@gmail.com>2019-02-08 11:39:53 +0200
commit7f153e936af6ae2ac1809e5758000aaf003a1f62 (patch)
tree24ebebc8a285affcce0054cf3072022f3ad4c090 /activerecord
parent44232b485485634d681c60868c619323f882e59f (diff)
downloadrails-7f153e936af6ae2ac1809e5758000aaf003a1f62.tar.gz
rails-7f153e936af6ae2ac1809e5758000aaf003a1f62.tar.bz2
rails-7f153e936af6ae2ac1809e5758000aaf003a1f62.zip
Bugfix has_many association #size when ids reader is cached and association is changed
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/associations/collection_association.rb4
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb15
2 files changed, 17 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb
index 6f5df807fe..c6941a2e76 100644
--- a/activerecord/lib/active_record/associations/collection_association.rb
+++ b/activerecord/lib/active_record/associations/collection_association.rb
@@ -348,7 +348,6 @@ module ActiveRecord
add_to_target(record) do
result = insert_record(record, true, raise) {
@_was_loaded = loaded?
- @association_ids = nil
}
end
raise ActiveRecord::Rollback unless result
@@ -385,6 +384,7 @@ module ActiveRecord
delete_records(existing_records, method) if existing_records.any?
@target -= records
+ @association_ids = nil
records.each { |record| callback(:after_remove, record) }
end
@@ -425,7 +425,6 @@ module ActiveRecord
unless owner.new_record?
result &&= insert_record(record, true, raise) {
@_was_loaded = loaded?
- @association_ids = nil
}
end
end
@@ -448,6 +447,7 @@ module ActiveRecord
if index
target[index] = record
elsif @_was_loaded || !loaded?
+ @association_ids = nil
target << record
end
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index 4c9e4d0ad2..130cb08b0d 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -2003,6 +2003,21 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert_not_predicate company.clients, :loaded?
end
+ def test_ids_reader_cache_not_used_for_size_when_association_is_dirty
+ firm = Firm.create!(name: "Startup")
+ assert_equal 0, firm.client_ids.size
+ firm.clients.build
+ assert_equal 1, firm.clients.size
+ end
+
+ def test_ids_reader_cache_should_be_cleared_when_collection_is_deleted
+ firm = companies(:first_firm)
+ assert_equal [2, 3, 11], firm.client_ids
+ client = firm.clients.first
+ firm.clients.delete(client)
+ assert_equal [3, 11], firm.client_ids
+ end
+
def test_zero_counter_cache_usage_on_unloaded_association
car = Car.create!(name: "My AppliCar")
assert_no_queries do