Skip to content
Browse files

Cache the association proxy object

This reimplements the behaviour of Rails 3, as I couldn't see why we
shouldn't cache the object, and @alindeman had a good use case for
caching it:

c86a32d#commitcomment-2784312
  • Loading branch information...
1 parent 0721d3b commit 133a1759a4c317e7836699bba2530603a84d7ccb @jonleighton jonleighton committed
View
2 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
View
5 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

0 comments on commit 133a175

Please sign in to comment.
Something went wrong with that request. Please try again.