Skip to content

Commit

Permalink
Removes caching from ActiveRecord::Core::ClassMethods#relation
Browse files Browse the repository at this point in the history
The #relation method gets called in four places and the return value was instantly cloned in three of them. The only place that did not clone was ActiveRecord::Scoping::Default::ClassMethods#unscoped. This introduced a bug described in rails#5667 and should really clone the relation, too. This means all four places would clone the relation, so it doesn't make a lot of sense caching it in the first place.

The four places with calls to relations are:

activerecord/lib/active_record/scoping/default.rb:110:in `block in build_default_scope'"
activerecord/lib/active_record/scoping/default.rb:42:in `unscoped'"
activerecord/lib/active_record/scoping/named.rb:38:in `scoped'"
activerecord/lib/active_record/scoping/named.rb:52:in `scope_attributes'"

Conflicts:

	activerecord/lib/active_record/core.rb
  • Loading branch information
benedikt committed Apr 19, 2012
1 parent 1166d49 commit 13f1401
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/scoping/named.rb
Expand Up @@ -34,7 +34,7 @@ def scoped(options = nil)
if current_scope
current_scope.clone
else
scope = relation.clone
scope = relation
scope.default_scoped = true
scope
end
Expand All @@ -48,7 +48,7 @@ def scope_attributes # :nodoc:
if current_scope
current_scope.scope_for_create
else
scope = relation.clone
scope = relation
scope.default_scoped = true
scope.scope_for_create
end
Expand Down
4 changes: 4 additions & 0 deletions activerecord/test/cases/relations_test.rb
Expand Up @@ -1088,6 +1088,10 @@ def test_unscoped_block_style
assert_equal 'honda', FastCar.unscoped { FastCar.order_using_old_style.limit(1).first.name}
end

def test_unscoped_relation_clones
assert_not_equal CoolCar.unscoped.object_id, CoolCar.unscoped.object_id
end

def test_intersection_with_array
relation = Author.where(:name => "David")
rails_author = relation.first
Expand Down

0 comments on commit 13f1401

Please sign in to comment.