Is there a way to combine named scopes into a new named scope?
Asked Answered
S

6

10

I have

class Foo < ActiveRecord::Base
  named_scope :a, lambda { |a| :conditions => { :a => a } }
  named_scope :b, lambda { |b| :conditions => { :b => b } }
end

I'd like

class Foo < ActiveRecord::Base
  named_scope :ab, lambda { |a,b| :conditions => { :a => a, :b => b } }
end

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

 Foo.a(something).b(something_else)

but it's not particularly lovely.

Skirret answered 26/8, 2008 at 20:13 Comment(1)
it seems that this behaviour is no default in rails.Talbert
F
2

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?


        def self.ab(a, b)
            a(a).b(b)
        end
    

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?

Let me know if I'm totally off base with what you're wanting to do.

Fleur answered 27/8, 2008 at 17:50 Comment(1)
With this solution it won't behave the scope as a normal scope does. For e.g. it won't be in Model.scopes.Fanya
D
15

At least since 3.2 there is a clever solution :

scope :optional, ->() {where(option: true)}
scope :accepted, ->() {where(accepted: true)}
scope :optional_and_accepted, ->() { self.optional.merge(self.accepted) }
Dichotomize answered 29/5, 2015 at 15:32 Comment(0)
F
2

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?


        def self.ab(a, b)
            a(a).b(b)
        end
    

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?

Let me know if I'm totally off base with what you're wanting to do.

Fleur answered 27/8, 2008 at 17:50 Comment(1)
With this solution it won't behave the scope as a normal scope does. For e.g. it won't be in Model.scopes.Fanya
R
1

By making it a class method you won't be able to chain it to an association proxy, like:

@category.products.ab(x, y)

An alternative is applying this patch to enable a :through option for named_scope:

named_scope :a, :conditions => {}
named_scope :b, :conditions => {}
named_scope :ab, :through => [:a, :b]
Roche answered 11/5, 2009 at 10:24 Comment(0)
W
1

Yes Reusing named_scope to define another named_scope

I copy it here for your convenience:

You can use proxy_options to recycle one named_scope into another:

class Thing
  #...
  named_scope :billable_by, lambda{|user| {:conditions => {:billable_id => user.id } } }
  named_scope :billable_by_tom, lambda{ self.billable_by(User.find_by_name('Tom').id).proxy_options }
  #...
end

This way it can be chained with other named_scopes.

I use this in my code and it works perfectly.

I hope it helps.

Wolff answered 20/7, 2010 at 12:53 Comment(1)
Caveat is that proxy_options only returns the scope of the latest named scope, so this cannot be done against another derived named scopeAcquisitive
S
0

@PJ: you know, I had considered that, but dismissed it because I thought I wouldn't be able to later chain on a third named scope, like so:

Foo.ab(x, y).c(z)

But since ab(x, y) returns whatever b(y) would return, I think the chain would work. Way to make me rethink the obvious!

Skirret answered 27/8, 2008 at 18:8 Comment(0)
A
0

Check out:

http://github.com/binarylogic/searchlogic

Impressive!

To be specific:

class Foo < ActiveRecord::Base
  #named_scope :ab, lambda { |a,b| :conditions => { :a => a, :b => b } }
  # alias_scope, returns a Scope defined procedurally
  alias_scope :ab, lambda {
    Foo.a.b
  }
end
Acquisitive answered 8/9, 2010 at 23:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.