# Searching on a complex attribute in my model

**URL:** <https://www.ruby-forum.com/t/searching-on-a-complex-attribute-in-my-model/160920>\
**Category:** Rails\
**Created:** [February 20, 2009, 11:03pm UTC](https://www.ruby-forum.com/t/searching-on-a-complex-attribute-in-my-model/160920 "2009-02-20T23:03:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![naijaguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9e8a1a/32.png) [@naijaguy](https://www.ruby-forum.com/u/naijaguy)\
**Post date:** [February 20, 2009, 11:03pm UTC](https://www.ruby-forum.com/t/searching-on-a-complex-attribute-in-my-model/160920/1 "2009-02-20T23:03:33Z")

</div>

Here is part of my User class:

has\_many :permissions  
has\_many :roles, :through =\> :permissions

This works very well, and I have a handy convenience method:

def has\_role?(name)  
self.roles.find\_by\_name(name) ? true : false  
end

However, I’m trying to figure out how to search the RoR way based on the  
role and some other criteria. For example, if I want to search for all  
users whose first name is Bob and have a role ‘admin’, what would I type  
in the conditions part of the query to filter by role? I know that with  
a User.find… query, I can no longer use the “self” keyword to call an  
instance method.

Thanks for any help…

---

<div class="post-metadata">

**Author:** ![naijaguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9e8a1a/32.png) [@naijaguy](https://www.ruby-forum.com/u/naijaguy)\
**Post date:** [February 20, 2009, 11:36pm UTC](https://www.ruby-forum.com/t/searching-on-a-complex-attribute-in-my-model/160920/2 "2009-02-20T23:36:22Z")

</div>

On Feb 20, 10:03 pm, Naija G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> However, I’m trying to figure out how to search the RoR way based on the  
> role and some other criteria. For example, if I want to search for all  
> users whose first name is Bob and have a role ‘admin’, what would I type  
> in the conditions part of the query to filter by role? I know that with  
> a User.find… query, I can no longer use the “self” keyword to call an  
> instance method.

You going to have to use a join so that you can have conditions on  
both tables eg

User.find :all, :joins =\> :roles, :conditions =\> ["roles.name = ? AND  
users.name = ? ", ‘admin’, ‘Bob’]

Or you also do it less verbosely by reversing that: Role.find\_by\_name  
(‘admin’).users.find :all, :conditions =\> {:name =\> ‘Bob’}

Fred

---

<div class="post-metadata">

**Author:** ![naijaguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/9e8a1a/32.png) [@naijaguy](https://www.ruby-forum.com/u/naijaguy)\
**Post date:** [February 21, 2009, 1:31am UTC](https://www.ruby-forum.com/t/searching-on-a-complex-attribute-in-my-model/160920/3 "2009-02-21T01:31:35Z")

</div>

Thanks a lot!

Frederick C. wrote:

> On Feb 20, 10:03ï¿½pm, Naija G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > However, I’m trying to figure out how to search the RoR way based on the  
> > role and some other criteria. ï¿½For example, if I want to search for all  
> > users whose first name is Bob and have a role ‘admin’, what would I type  
> > in the conditions part of the query to filter by role? ï¿½I know that with  
> > a User.find… query, I can no longer use the “self” keyword to call an  
> > instance method.
> 
> You going to have to use a join so that you can have conditions on  
> both tables eg
> 
> User.find :all, :joins =\> :roles, :conditions =\> ["roles.name = ? AND  
> users.name = ? ", ‘admin’, ‘Bob’]
> 
> Or you also do it less verbosely by reversing that: Role.find\_by\_name  
> (‘admin’).users.find :all, :conditions =\> {:name =\> ‘Bob’}
> 
> Fred
