Skip to content

Commit

Permalink
Remove duplication of conditions generated for associations when used…
Browse files Browse the repository at this point in the history
… in conjunction with named_scopes [#4634 state: resolved]
  • Loading branch information
marklazz authored and tenderlove committed Sep 30, 2010
1 parent 5a63df2 commit 9b78af9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/named_scope.rb
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/named_scope_test.rb
Expand Up @@ -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?
Expand Down

1 comment on commit 9b78af9

@masterkain
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

finally!
awesome, thanks ;)

Please sign in to comment.