Search form with act_as_taggable_on (Rails 3)

I have a search query to search for products. Each product has a title and is tagged with several tags.

I want to be able to search for products by name or tag. In other words, if I have a product called "Green Tea" and another product is labeled "green, red, blue" and I type "green" in the search field, I would like both products to appear in the search results.

I am using Rails 3, actions_as_taggable_on, Ruby 1.9.2.

+1
source share
1 answer

In your controller, the action that displays the search results may look something like this (where: q is the query string from the search box):

def results @products = Product.where("title LIKE ?", "%#{params[:q]}%") \ | Product.tagged_with("%#{params[:q]}%") end 
+8
source

Source: https://habr.com/ru/post/1396031/


All Articles