1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #21092 from vngrs/use_memoization_for_ids_reader

Use memoization for collection associations ids reader
This commit is contained in:
Rafael Mendonça França 2015-08-06 17:38:36 -03:00
commit 22625a353e
2 changed files with 12 additions and 2 deletions

View file

@ -62,8 +62,10 @@ module ActiveRecord
record.send(reflection.association_primary_key)
end
else
@association_ids ||= (
column = "#{reflection.quoted_table_name}.#{reflection.association_primary_key}"
scope.pluck(column)
)
end
end

View file

@ -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