# Search across associations

**URL:** <https://www.ruby-forum.com/t/search-across-associations/123920>\
**Category:** Rails\
**Created:** [December 22, 2007, 1:41am UTC](https://www.ruby-forum.com/t/search-across-associations/123920 "2007-12-22T01:41:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [December 22, 2007, 1:41am UTC](https://www.ruby-forum.com/t/search-across-associations/123920/1 "2007-12-22T01:41:20Z")

</div>

Hello:

I’m pretty new to mysql and need to build a search function that  
searches fields in a ‘case’ object as well as fields in ‘person’ objects  
that reference each ‘case’ object. The function must return a single  
array of ‘case’ objects.

Searching for fields in the case object seems pretty straightforward:

def search  
@cases = Case.find(:all, :conditions =\> [“lower(field\_1) like ? OR  
lower(field\_2) like ?”, “%” + params[:search].downcase + “%”, “%” +  
params[:search].downcase + “%”])  
render :partial =\> ‘cases’, :collection =\> @cases  
end

But I can’t figure out how to search objects that ‘belong\_to’ the case  
object.

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [December 22, 2007, 3:32am UTC](https://www.ruby-forum.com/t/search-across-associations/123920/2 "2007-12-22T03:32:02Z")

</div>

> ## But I can’t figure out how to search objects that ‘belong\_to’ the case object.
> 
> Posted viahttp://www.ruby-forum.com/.

It seems like what you are trying to do is search the person fields  
for a string. That is going to return Person records. You then want  
an array of the cases associated with the persons that were returned  
from the search string. Is that correct?

Greg DeVore

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [December 22, 2007, 3:42am UTC](https://www.ruby-forum.com/t/search-across-associations/123920/3 "2007-12-22T03:42:01Z")

</div>

On 22 Dec 2007, at 00:41, Peter M. wrote:

> def search  
> @cases = Case.find(:all, :conditions =\> [“lower(field\_1) like ? OR  
> lower(field\_2) like ?”, “%” + params[:search].downcase + “%”, “%” +  
> params[:search].downcase + “%”])  
> render :partial =\> ‘cases’, :collection =\> @cases  
> end
> 
> But I can’t figure out how to search objects that ‘belong\_to’ the case  
> object.

You can do Case.find :all, :include =\> :person, :conditions =\>  
[‘cases.foo = ? or people.bar = ?’, …]  
In rails 2 you can use :joins instead of :include if you’re not  
interested in actually eager loading the :person associations.  
And you’ve always been able to Case.find :all, :joins =\> “some fancy  
bit of sql to join the people table in the desired way”, :conditions  
=\> [‘cases.foo = ? or people.bar = ?’, …]  
(you almost always want to specify a :select option if you do tha)

Fred

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [December 22, 2007, 4:29am UTC](https://www.ruby-forum.com/t/search-across-associations/123920/4 "2007-12-22T04:29:33Z")

</div>

Frederick C. wrote:

> You can do Case.find :all, :include =\> :person, :conditions =\>  
> [‘cases.foo = ? or people.bar = ?’, …]  
> In rails 2 you can use :joins instead of :include if you’re not  
> interested in actually eager loading the :person associations.  
> And you’ve always been able to Case.find :all, :joins =\> “some fancy  
> bit of sql to join the people table in the desired way”, :conditions  
> =\> [‘cases.foo = ? or people.bar = ?’, …]  
> (you almost always want to specify a :select option if you do tha)
> 
> Fred

This is exactly what I was looking for. God I love rails. Thanks a lot  
guys.

Peter
