# Searching parent objects and associated objects with one search

**URL:** <https://www.ruby-forum.com/t/searching-parent-objects-and-associated-objects-with-one-search/151140>\
**Category:** Rails\
**Created:** [October 17, 2008, 10:02am UTC](https://www.ruby-forum.com/t/searching-parent-objects-and-associated-objects-with-one-search/151140 "2008-10-17T10:02:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yogi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/46a35a/32.png) [@Yogi](https://www.ruby-forum.com/u/Yogi)\
**Post date:** [October 17, 2008, 10:02am UTC](https://www.ruby-forum.com/t/searching-parent-objects-and-associated-objects-with-one-search/151140/1 "2008-10-17T10:02:58Z")

</div>

Three simple models setup with :through association

Person  
has\_many :readings  
has\_many :books, :through =\> :readings

Reading  
belongs\_to :person  
belongs\_to :book

Book  
has\_many :readings  
has\_many :persons, :through =\> :readings

I want to search persons names and all his books names and return  
matching persons. how do i correctly form a search like this  
syntatically wrong one

Person.find(:all, :conditions =\> [“person.name LIKE ? and book.name  
LIKE ?”, “%#{params[:person\_name]}%”, “%#{params[:book\_name]}%”])

tia,  
yogi

---

<div class="post-metadata">

**Author:** ![Yogi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/46a35a/32.png) [@Yogi](https://www.ruby-forum.com/u/Yogi)\
**Post date:** [October 17, 2008, 11:03am UTC](https://www.ruby-forum.com/t/searching-parent-objects-and-associated-objects-with-one-search/151140/2 "2008-10-17T11:03:39Z")

</div>

Yogi wrote:

> belongs\_to :book
> 
> Person.find(:all, :conditions =\> [“person.name LIKE ? and book.name  
> LIKE ?”, “%#{params[:person\_name]}%”, “%#{params[:book\_name]}%”])

That looks syntactically correct. It just needs a little tweaking:

Person.find(:all, :select =\> ‘DISTINCT people.\*’, :joins =\> :books,  
:conditions =\> [“people.name LIKE ? AND books.name LIKE ?”,  
“%#{params[:person\_name]}%”, “%#{params[:book\_name]}%”]

–  
Rails Wheels - Find Plugins, List & Sell Plugins -  
[http://railswheels.com](http://railswheels.com)

---

<div class="post-metadata">

**Author:** ![Yogi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/46a35a/32.png) [@Yogi](https://www.ruby-forum.com/u/Yogi)\
**Post date:** [October 19, 2008, 11:43am UTC](https://www.ruby-forum.com/t/searching-parent-objects-and-associated-objects-with-one-search/151140/3 "2008-10-19T11:43:03Z")

</div>

Thanks for Mark for the ‘tuning’. Now that I think of this, can I add  
another associated table (reviews) and include that into the search  
also? What I mean is that if the models are

Person  
has\_many :readings  
has\_many :books, :through =\> :readings  
has\_many :reviews

Reading  
belongs\_to :person  
belongs\_to :book

Book  
has\_many :readings  
has\_many :persons, :through =\> :readings

Review  
belongs\_to :person

Now I would like to form a search like below

Person.find(:all,  
:conditions =\> [“people.name LIKE ? AND  
books.name LIKE ? AND  
reviews.rating LIKE ?”,  
“%#{params[:person\_name]}%”,  
“%#{params[:book\_name]}%”,  
“%#{params[:rating]}%”])

How would I use :joins to make the above to work?

tia,  
yogi

---

<div class="post-metadata">

**Author:** ![Yogi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/46a35a/32.png) [@Yogi](https://www.ruby-forum.com/u/Yogi)\
**Post date:** [October 20, 2008, 10:30pm UTC](https://www.ruby-forum.com/t/searching-parent-objects-and-associated-objects-with-one-search/151140/4 "2008-10-20T22:30:55Z")

</div>

Yogi wrote:

> ```
> belongs_to :person
> 
> ```
> 
> Now I would like to form a search like below
> 
> Person.find(:all,  
> :conditions =\> [“people.name LIKE ? AND  
> books.name LIKE ? AND  
> reviews.rating LIKE ?”,  
> “%#{params[:person\_name]}%”,  
> “%#{params[:book\_name]}%”,  
> “%#{params[:rating]}%”])
> 
> How would I use :joins to make the above to work?

Use the :joins option just like :include. So

:joins =\> [:books, :reviews]

–  
Rails Wheels - Find Plugins, List & Sell Plugins -  
[http://railswheels.com](http://railswheels.com)
