Permalink
Browse files

Named scopes shouldn't test equality using to_a if it's not an Array,…

… this was causing records to be loaded before they were needed.

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
1 parent 41b45d5 commit 8d99ec9a4dcef5918c4487a0d94ef0a9622fe8c9 @miloops miloops committed with josevalim Mar 30, 2010
Showing with 17 additions and 4 deletions.
  1. +8 −1 activerecord/lib/active_record/named_scope.rb
  2. +9 −3 activerecord/test/cases/named_scope_test.rb
@@ -167,7 +167,14 @@ def last(*args)
end
def ==(other)
- other.respond_to?(:to_ary) ? to_a == other.to_a : false
+ case other
+ when Scope
+ to_sql == other.to_sql
+ when Relation
+ other == self
+ when Array
+ to_a == other.to_a
+ end
end
private
@@ -374,14 +374,14 @@ def test_named_scopes_with_reserved_names
class << Topic
def public_method; end
public :public_method
-
+
def protected_method; end
protected :protected_method
-
+
def private_method; end
private :private_method
end
-
+
[:public_method, :protected_method, :private_method].each do |reserved_method|
assert Topic.respond_to?(reserved_method, true)
ActiveRecord::Base.logger.expects(:warn)
@@ -407,6 +407,12 @@ def test_index_on_named_scope
assert_equal topics(:second), approved[0]
assert approved.loaded?
end
+
+ def test_nested_named_scopes_queries_size
+ assert_queries(1) do
+ Topic.approved.by_lifo.replied.written_before(Time.now).all
+ end
+ end
end
class DynamicScopeMatchTest < ActiveRecord::TestCase

0 comments on commit 8d99ec9

Please sign in to comment.