Search code examples
ruby-on-railsruby-on-rails-3.1ruby-on-rails-3.2

modifying the query for returning a list of products in a category, right now it returns the list of products based on name


I have a list of products and a list of categories. Now the products and categories have a many to many relationship.

I have the following code that searches through the list and finds all the products that are being searched..

if params[:search]
  @products = Product.find(:all, :conditions => ['name LIKE ?', "%#{params[:search]}%"])
else
 @products = Product.all
end

How to convert this to return me a list of products of a category?

That is

@products = Product.find(:all, :conditions => ["what should be here"])

or any other query all together

The code is at https://github.com/abhishekdagarit/store.git

In the code category is called group


Solution

  • @products = Product.find(:all, :include => :group, :conditions => ['name ILIKE ? OR group.products ILIKE ?', "%#{search}%", "%#{search}%" ])