Skip to content

Commit

Permalink
Cache the association proxy object
Browse files Browse the repository at this point in the history
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
jonleighton committed Mar 15, 2013
1 parent 0721d3b commit 133a175
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions activerecord/test/cases/associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 133a175

Please sign in to comment.