dexx1220
dexx1220

Reputation: 101

Ruby on Rails: how to use find on multiple conditions and multiple tables?

I have a product model and a category model. Product has_many_and_belongs_to Category, and vice-versa. Now I want to use Product.find to search for a Product.name but also a Product.categories. I tried something like this but it didn't work:

Product.find(:all, :conditions => ['name LIKE ? or categories LIKE ?', '%#{keyword}%']

Basically, I'm trying to get results for products with name like 'car' or products with a category of 'car'. Thanks in advance!

Upvotes: 0

Views: 332

Answers (1)

Konstantin Ilchenko
Konstantin Ilchenko

Reputation: 485

Product.joins(:categories).where('products.name LIKE ? or categories.name LIKE ?', "%#{keyword}")

I think something like this can help you if you using rails > 3

Upvotes: 2

Related Questions