提交 22625a35 编写于 作者: R Rafael Mendonça França

Merge pull request #21092 from vngrs/use_memoization_for_ids_reader

Use memoization for collection associations ids reader
......@@ -62,8 +62,10 @@ def ids_reader
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
......
......@@ -2308,4 +2308,12 @@ def test_destroy_with_bang_bubbles_errors_from_associations
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册