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) 

see: specifying conditions on eager loaded associations


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -