mysql - Rails search through `has_many :through` text fields -
let have 2 models association has_many :through
between them.
class category < activerecord::base has_many :category_recipes has_many :categories, through: :category_recipes validates :name, presence: true class recipe < activerecord::base has_many :category_recipes has_many :categories, through: :category_recipes validates :title, presence: true
i want create search functionality using activerecord mysql database, allow users implement text search on recipe
title , category name
.
now have just:
@recipes = recipe.where('title ?', "%#{params[:query]}%")
how can modify query search through both title of recipe , it's category names?
recipe.includes(:categories).where('recipes.title :query or categories.name :query', query: "%#{params[:query]}%").references(:categories)
Comments
Post a Comment