Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Removes caching from ActiveRecord::Core::ClassMethods#relation

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 #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'"
  • Loading branch information...
commit 68677ffb8298105eb9d3efa26d928dd88cc5e006 1 parent 0cc6c5f
@benedikt benedikt authored
View
7 activerecord/lib/active_record/core.rb
@@ -137,12 +137,12 @@ def arel_engine
private
def relation #:nodoc:
- @relation ||= Relation.new(self, arel_table)
+ relation = Relation.new(self, arel_table)
if finder_needs_type_condition?
- @relation.where(type_condition).create_with(inheritance_column.to_sym => sti_name)
+ relation.where(type_condition).create_with(inheritance_column.to_sym => sti_name)
else
- @relation
+ relation
end
end
end
@@ -351,7 +351,6 @@ def init_internals
@attributes[pk] = nil unless @attributes.key?(pk)
- @relation = nil
@aggregation_cache = {}
@association_cache = {}
@attributes_cache = {}
View
4 activerecord/lib/active_record/scoping/named.rb
@@ -35,7 +35,7 @@ def scoped(options = nil)
if current_scope
current_scope.clone
else
- scope = relation.clone
+ scope = relation
scope.default_scoped = true
scope
end
@@ -49,7 +49,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
View
4 activerecord/test/cases/relations_test.rb
@@ -1099,6 +1099,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
Please sign in to comment.
Something went wrong with that request. Please try again.