<p>I have</p>

<pre><code>class Foo &lt; ActiveRecord::Base
  named_scope :a, lambda { |a| :conditions =&gt; { :a =&gt; a } }
  named_scope :b, lambda { |b| :conditions =&gt; { :b =&gt; b } }
end
</code></pre>

<p>I'd like</p>

<pre><code>class Foo &lt; ActiveRecord::Base
  named_scope :ab, lambda { |a,b| :conditions =&gt; { :a =&gt; a, :b =&gt; b } }
end
</code></pre>

<p>but I'd prefer to do it in a DRY fashion.  I can get the same effect by using</p>

<pre><code> Foo.a(something).b(something_else)
</code></pre>

<p>but it's not particularly lovely.</p>

[{'answer_id': 30719, 'author': 'PJ.', 'author_id': 3230, 'author_profile': 'https://Stackoverflow.com/users/3230', 'pm_score': 3, 'selected': True, 'text': "<p>Well I'm still new to rails and I'm not sure exactly what you're going for here, but if you're just going for code reuse why not use a regular class method? \n<pre><code>\n        def self.ab(a, b)\n            a(a).b(b)\n        end\n    </pre></code></p>\n\n<p>You could make that more flexible by taking *args instead of a and b, and then possibly make one or the other optional.  If you're stuck on named_scope, can't you extend it to do much the same thing?  </p>\n\n<p>Let me know if I'm totally off base with what you're wanting to do. </p>\n"}, {'answer_id': 30753, 'author': 'James A. Rosen', 'author_id': 1190, 'author_profile': 'https://Stackoverflow.com/users/1190', 'pm_score': 0, 'selected': False, 'text': '<p>@<a href="https://stackoverflow.com/questions/29011/is-there-a-way-to-combine-named-scopes-into-a-new-named-scope#30719">PJ</a>: you know, I had considered that, but dismissed it because I thought I wouldn\'t be able to later chain on a <em>third</em> named scope, like so:</p>\n\n<pre><code>Foo.ab(x, y).c(z)\n</code></pre>\n\n<p>But since <code>ab(x, y)</code> returns whatever <code>b(y)</code> would return, I think the chain would work.  Way to make me rethink the obvious!</p>\n'}, {'answer_id': 847512, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>By making it a class method you won\'t be able to chain it to an association proxy, like:</p>\n\n<pre><code>@category.products.ab(x, y)\n</code></pre>\n\n<p>An alternative is applying <a href="https://rails.lighthouseapp.com/projects/8994/tickets/57-through-option-for-named_scope" rel="nofollow noreferrer">this patch</a> to enable a :through option for named_scope:</p>\n\n<pre><code>named_scope :a, :conditions =&gt; {}\nnamed_scope :b, :conditions =&gt; {}\nnamed_scope :ab, :through =&gt; [:a, :b]\n</code></pre>\n'}, {'answer_id': 3290095, 'author': 'Oinak', 'author_id': 97635, 'author_profile': 'https://Stackoverflow.com/users/97635', 'pm_score': 1, 'selected': False, 'text': '<p>Yes <a href="https://stackoverflow.com/questions/2443650/reusing-named-scope-to-define-another-named-scope/3290077#3290077">Reusing named_scope to define another named_scope</a></p>\n\n<p>I copy it here for your convenience:</p>\n\n<p>You can use proxy_options to recycle one named_scope into another:</p>\n\n<pre><code>class Thing\n  #...\n  named_scope :billable_by, lambda{|user| {:conditions =&gt; {:billable_id =&gt; user.id } } }\n  named_scope :billable_by_tom, lambda{ self.billable_by(User.find_by_name(\'Tom\').id).proxy_options }\n  #...\nend\n</code></pre>\n\n<p>This way it can be chained with other named_scopes.</p>\n\n<p>I use this in my code and it works perfectly.</p>\n\n<p>I hope it helps.</p>\n'}, {'answer_id': 3672581, 'author': 'aceofspades', 'author_id': 237150, 'author_profile': 'https://Stackoverflow.com/users/237150', 'pm_score': 0, 'selected': False, 'text': '<p>Check out:</p>\n\n<p><a href="http://github.com/binarylogic/searchlogic" rel="nofollow noreferrer">http://github.com/binarylogic/searchlogic</a></p>\n\n<p>Impressive!</p>\n\n<p>To be specific:</p>\n\n<pre><code>class Foo &lt; ActiveRecord::Base\n  #named_scope :ab, lambda { |a,b| :conditions =&gt; { :a =&gt; a, :b =&gt; b } }\n  # alias_scope, returns a Scope defined procedurally\n  alias_scope :ab, lambda {\n    Foo.a.b\n  }\nend\n</code></pre>\n'}, {'answer_id': 30533540, 'author': 'Meta Lambda', 'author_id': 2481743, 'author_profile': 'https://Stackoverflow.com/users/2481743', 'pm_score': 4, 'selected': False, 'text': '<p>At least since 3.2 there is a clever solution :</p>\n\n<pre><code>scope :optional, -&gt;() {where(option: true)}\nscope :accepted, -&gt;() {where(accepted: true)}\nscope :optional_and_accepted, -&gt;() { self.optional.merge(self.accepted) }\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/29011', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1190/']