# Chained search

**URL:** <https://www.ruby-forum.com/t/chained-search/162375>\
**Category:** Rails\
**Created:** [March 9, 2009, 10:50am UTC](https://www.ruby-forum.com/t/chained-search/162375 "2009-03-09T10:50:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pesho318i](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pesho318i](https://www.ruby-forum.com/u/pesho318i)\
**Post date:** [March 9, 2009, 10:50am UTC](https://www.ruby-forum.com/t/chained-search/162375/1 "2009-03-09T10:50:08Z")

</div>

Hello,

I have:  
Messages table with membership\_id column  
Memberships table with member\_id column  
Members table with name column

Having a single message m, how do I get the name of the member of the  
membership of this message?

The following works ok:  
Member.find(Membership.find(m.membership\_id).member\_id).name

but it seems to me it’s unnecessarily complicated…

---

<div class="post-metadata">

**Author:** ![pesho318i](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pesho318i](https://www.ruby-forum.com/u/pesho318i)\
**Post date:** [March 9, 2009, 2:39pm UTC](https://www.ruby-forum.com/t/chained-search/162375/2 "2009-03-09T14:39:39Z")

</div>

On 9 Mar 2009, at 09:50, Pesho P. wrote:

> The following works ok:  
> Member.find(Membership.find(m.membership\_id).member\_id).name

setup the usual associations (if this makes no sense to you then have  
a look at [Active Record Associations — Ruby on Rails Guides](http://guides.rubyonrails.org/association_basics.html))

and then  
m.membership.member.name

Fred

---

<div class="post-metadata">

**Author:** ![pesho318i](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pesho318i](https://www.ruby-forum.com/u/pesho318i)\
**Post date:** [March 10, 2009, 5:43pm UTC](https://www.ruby-forum.com/t/chained-search/162375/3 "2009-03-10T17:43:24Z")

</div>

Frederick C. wrote:

> setup the usual associations (if this makes no sense to you then have  
> a look at [Active Record Associations — Ruby on Rails Guides](http://guides.rubyonrails.org/association_basics.html))
> 
> and then  
> m.membership.member.name

Thanks, Frederick! I got it working.  
However, now I need to do a more complicated/weird query…

## In my model:

Message (fields: membership\_id, is\_pending)  
belongs\_to Membership

Membership (fields: member\_id, team\_id, role\_id)  
belongs\_to Member  
belongs\_to Group  
belongs\_to Role

all the reverse associations are has\_many

I would like to find all pending Messages, which belong to Memberships,  
whose group\_id is one of the group\_id-s where a certain Member m has a  
Role “admin”

In other words, each Member is in a Group, and has a Role in this  
Group(the triples member-group-role are defined in the model  
Membership). On the other hand, each Message is associated to a  
Membership. If we look at the two sets:

1. All Messages with is\_pending=true
2. All the Memberships where Member m has role “admin”  
… then I need the messages from (1.), which belong to the Memberships  
from (2.)

I realize it’s quite complicated, but if you can suggest me another way  
to achieve this, I’ll be happy to change my approach.

Thanks!  
Petar

---

<div class="post-metadata">

**Author:** ![pesho318i](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pesho318i](https://www.ruby-forum.com/u/pesho318i)\
**Post date:** [March 10, 2009, 6:00pm UTC](https://www.ruby-forum.com/t/chained-search/162375/4 "2009-03-10T18:00:49Z")

</div>

On Mar 10, 4:43 pm, Pesho P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> to achieve this, I’ll be happy to change my approach.

One way to do this is to join the tables which have the extra  
information so that you can then put conditions on those. So at a  
basic level if it was just messages and memberships the raw sql would  
be something like

select messages.\* from messages  
inner join memberships on memberships.id = messages.membership\_id  
where some conditions on messages and memberships.

You don’t need to write out all this, Message.find :all, :joins  
=\> :memberships, :conditions =\> […]

You can join several layers down - there’s some examples at

> **[Nested includes and joins - Space Vatican](https://www.spacevatican.org/2008/8/8/nested-includes-and-joins/)**
>
> Nested eager loads are a not entirely obvious bit of rails syntax. It’s not hard once you get it though. In a nutshell you have to tell …

Fred
