Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Remove duplication of conditions generated for associations when used…

… in conjunction with named_scopes [#4634 state: resolved]
  • Loading branch information...
commit 9b78af95be01534b3f1a4187024d803313c0ee1f 1 parent 5a63df2
@marklazz marklazz authored tenderlove committed
View
2  activerecord/lib/active_record/named_scope.rb
@@ -120,7 +120,7 @@ def initialize(proxy_scope, options, &block)
options ||= {}
[options[:extend]].flatten.each { |extension| extend extension } if options[:extend]
extend Module.new(&block) if block_given?
- unless Scope === proxy_scope
+ unless (Scope === proxy_scope || ActiveRecord::Associations::AssociationCollection === proxy_scope)
@current_scoped_methods_when_defined = proxy_scope.send(:current_scoped_methods)
end
@proxy_scope, @proxy_options = proxy_scope, options.except(:extend)
View
6 activerecord/test/cases/named_scope_test.rb
@@ -146,6 +146,12 @@ def test_has_many_associations_have_access_to_named_scopes
assert_equal authors(:david).posts & Post.containing_the_letter_a, authors(:david).posts.containing_the_letter_a
end
+ def test_nested_named_scopes_doesnt_duplicate_conditions_on_child_scopes
+ comments_scope = posts(:welcome).comments.send(:construct_sql)
+ named_scope_sql_conditions = posts(:welcome).comments.containing_the_letter_e.send(:current_scoped_methods)[:find][:conditions]
+ assert_no_match /#{comments_scope}.*#{comments_scope}/i, named_scope_sql_conditions
+ end
+
def test_has_many_through_associations_have_access_to_named_scopes
assert_not_equal Comment.containing_the_letter_e, authors(:david).comments
assert !Comment.containing_the_letter_e.empty?

1 comment on commit 9b78af9

@masterkain

finally!
awesome, thanks ;)

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