diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-08-06 17:38:36 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-08-06 17:38:36 -0300 |
commit | 22625a353e8d9613f615a25ec642eb057de19cf1 (patch) | |
tree | 854ce96cd60a4c2d711a27becffc01bc4c51ddd0 | |
parent | 7c23c38b3317da7dee166ddaa5e492aa1d7b5759 (diff) | |
parent | f744d627364a9a98dedda5b30711bf80ebc3451f (diff) | |
download | rails-22625a353e8d9613f615a25ec642eb057de19cf1.tar.gz rails-22625a353e8d9613f615a25ec642eb057de19cf1.tar.bz2 rails-22625a353e8d9613f615a25ec642eb057de19cf1.zip |
Merge pull request #21092 from vngrs/use_memoization_for_ids_reader
Use memoization for collection associations ids reader
-rw-r--r-- | activerecord/lib/active_record/associations/collection_association.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/associations/has_many_associations_test.rb | 8 |
2 files changed, 12 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index 87576abd92..0fc2b83b71 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -62,8 +62,10 @@ module ActiveRecord record.send(reflection.association_primary_key) end else - column = "#{reflection.quoted_table_name}.#{reflection.association_primary_key}" - scope.pluck(column) + @association_ids ||= ( + column = "#{reflection.quoted_table_name}.#{reflection.association_primary_key}" + scope.pluck(column) + ) 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 21e2ee3b11..2cbee3cd21 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -2308,4 +2308,12 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_instance_of PostWithErrorDestroying, error.record end + + def test_ids_reader_memoization + car = Car.create!(name: 'Tofaş') + bulb = Bulb.create!(car: car) + + assert_equal [bulb.id], car.bulb_ids + assert_no_queries { car.bulb_ids } + end end |