# Named scopes, chaining and instance methods

**URL:** <https://www.ruby-forum.com/t/named-scopes-chaining-and-instance-methods/186872>\
**Category:** Rails\
**Created:** [April 13, 2010, 5:32pm UTC](https://www.ruby-forum.com/t/named-scopes-chaining-and-instance-methods/186872 "2010-04-13T17:32:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/bb73d2/32.png) [@ian](https://www.ruby-forum.com/u/ian)\
**Post date:** [April 13, 2010, 5:32pm UTC](https://www.ruby-forum.com/t/named-scopes-chaining-and-instance-methods/186872/1 "2010-04-13T17:32:36Z")

</div>

In my app we have lists of people that we allow users to filter a  
variety of ways. For example, a user might choose 3 filters: Enrolled,  
Living and Lives in Wyoming.

To handle this, I’ve been using named scopes. The Person model has  
lines like this:

named\_scope :enrolled, :conditions =\> “blah”  
named\_scope :living, :conditions =\> “blah”  
named\_scope :lives\_in\_state, lambda { |s| {:conditions =\> [“state  
= ?”, s]}}

And then when getting the People to display in the view, I’ll have:

@people = People.enrolled.living.lives\_in\_state(“WY”).paginate

So far this has worked well because I’m filtering based on pretty  
straightforward data requirements.

But now I’m implementing a new filter that relies not on the Person  
data, but on a Person instance method.

class Person

def evaluation\_score  
…logic to determine score  
end  
end

Say I want to filter people that have an evaluation score of 80 or  
higher. How can I integrate this with the named route solution that  
I’ve already implemented. What I’d like is to be able to do something  
like this:

@people =  
People.enrolled.living.lives\_in\_state(“WY”).evaluation\_score\_over(80).paginate

Right now I’ve just added a new class method

class Person

def Person.evaluation\_score\_over(minimum\_score)  
self.all.reject {|l| l.evaluation\_score \< minimum\_score}  
end

def evaluation\_score  
… logic to determine score  
end  
end

And this works. But it also breaks the chaining I can do with named  
scopes. So if I try this:

People.evaluation\_score\_over(80).enrolled.living.lives\_in\_state(“WY”).paginate

it fails. evaluation\_score\_over returns an array and the array doesn’t  
have a method “enrolled”.

Is there a way I can implement the kind of filtering I need to do and  
still get all the chaining benefits of named scopes?

---

<div class="post-metadata">

**Author:** ![ian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/bb73d2/32.png) [@ian](https://www.ruby-forum.com/u/ian)\
**Post date:** [April 13, 2010, 7:30pm UTC](https://www.ruby-forum.com/t/named-scopes-chaining-and-instance-methods/186872/2 "2010-04-13T19:30:21Z")

</div>

On Apr 13, 4:30 pm, Ian [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> And this works. But it also breaks the chaining I can do with named  
> scopes. So if I try this:
> 
> People.evaluation\_score\_over(80).enrolled.living.lives\_in\_state(“WY”).pagin ate
> 
> it fails. evaluation\_score\_over returns an array and the array doesn’t  
> have a method “enrolled”.
> 
> Is there a way I can implement the kind of filtering I need to do and  
> still get all the chaining benefits of named scopes?

Fundamentally you need to be able to reduce it to a set of options to  
find - that’s all named\_scopes know how do deal with.

Fred
