# Like Query Across Multiple Tables

**URL:** <https://www.ruby-forum.com/t/like-query-across-multiple-tables/53884>\
**Category:** Rails\
**Created:** [February 15, 2006, 11:30pm UTC](https://www.ruby-forum.com/t/like-query-across-multiple-tables/53884 "2006-02-15T23:30:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![danhobbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a5b964/32.png) [@danhobbs](https://www.ruby-forum.com/u/danhobbs)\
**Post date:** [February 15, 2006, 11:30pm UTC](https://www.ruby-forum.com/t/like-query-across-multiple-tables/53884/1 "2006-02-15T23:30:12Z")

</div>

Firstly, I’m still learning Ruby and rails, so there could be a very  
simple solution to this.

I want to search across multiple tables with a “like” query eg: (SQL is  
probably the easiest way to describe it):

select  
products.\*  
from products  
inner join brands on products.brand\_id = brands.id  
where  
products.name like ‘%canon%’  
or brands.name like ‘%canon%’

Currently, I have a solution for only the products table. In the  
product.rb I have:  
def self.search(search\_string)  
find(:all,  
:conditions =\> [“lower(products.name) like ?”,  
“%#{search\_string.downcase}%”])  
end

Which works fine, is there an easy way to add the search to include  
brands?

Thanks,  
Dan

–

> **[Kingdom Solutions](https://www.kingdomsolutions.com.au/)**
>
> Software Engineering, DevOps and Cloud Architecture

---

<div class="post-metadata">

**Author:** ![danhobbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a5b964/32.png) [@danhobbs](https://www.ruby-forum.com/u/danhobbs)\
**Post date:** [February 16, 2006, 2:19am UTC](https://www.ruby-forum.com/t/like-query-across-multiple-tables/53884/2 "2006-02-16T02:19:39Z")

</div>

Dan H. wrote:

> Currently, I have a solution for only the products table. In the  
> product.rb I have:  
> def self.search(search\_string)  
> find(:all,  
> :conditions =\> [“lower(products.name) like ?”,  
> “%#{search\_string.downcase}%”])  
> end
> 
> Which works fine, is there an easy way to add the search to include  
> brands?  
> No ideas on this one guys?

I’ve been scouring through the Agile Rails book, but I can’t seem to  
find any reference that fits my need. Any ideas?

Thanks,  
Dan

–

> **[Kingdom Solutions](https://www.kingdomsolutions.com.au/)**
>
> Software Engineering, DevOps and Cloud Architecture

---

<div class="post-metadata">

**Author:** ![danhobbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a5b964/32.png) [@danhobbs](https://www.ruby-forum.com/u/danhobbs)\
**Post date:** [February 17, 2006, 9:00am UTC](https://www.ruby-forum.com/t/like-query-across-multiple-tables/53884/3 "2006-02-17T09:00:19Z")

</div>

With :include =\> you can use like on the other table as well:

def self.search(keyword)  
find(:all, :include =\> :brand, :conditions =\> [‘products.name like ?  
or brands.name like ?’, “%#{keyword}%”, “%#{keyword}%”]  
end

The result will be an array of Product objects. If you want brand  
objects, just implement a similar search on the Brand class.

-Jonny.

---

<div class="post-metadata">

**Author:** ![danhobbs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a5b964/32.png) [@danhobbs](https://www.ruby-forum.com/u/danhobbs)\
**Post date:** [February 16, 2006, 3:01am UTC](https://www.ruby-forum.com/t/like-query-across-multiple-tables/53884/4 "2006-02-16T03:01:55Z")

</div>

You can use any custom SQL you’d like with AR’s find\_by\_sql:

Product.find\_by\_sql(“select \* from products where 1=1”)

The fact that you use a LIKE clause should have no effect on this.

HTH.

On Thursday, February 16, 2006, at 12:16 PM, Dan H. wrote:

> > brands?
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Cheers!  
–Dave T.  
[http://devlists.com](http://devlists.com) - Email list management  
[http://palmsphere.com](http://palmsphere.com) - Apps for your hand-held
