diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index 18b7dc3668ceee8069b2a61349cba4b91d96f6df..906560bd44c82eea1fec8b15001a4e9b72c588dc 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -34,7 +34,7 @@ def reader(force_reload = false) reload end - CollectionProxy.new(klass, self) + @proxy ||= CollectionProxy.new(klass, self) end # Implements the writer method, e.g. foo.items= for Foo.has_many :items diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb index 41529cd050e190f395a33cb9289909f005d34db4..a06bacafca7b758b61f70ced319a16ad950903d7 100644 --- a/activerecord/test/cases/associations_test.rb +++ b/activerecord/test/cases/associations_test.rb @@ -237,6 +237,11 @@ def test_scoped_allows_conditions assert david.projects.scope.is_a?(ActiveRecord::Relation) assert_equal david.projects, david.projects.scope end + + test "proxy object is cached" do + david = developers(:david) + assert david.projects.equal?(david.projects) + end end class OverridingAssociationsTest < ActiveRecord::TestCase