Ruby on Rails - search in database based on a query

前端 未结 4 913
灰色年华
灰色年华 2021-01-26 05:54

I have a simple form, where I set up a query that I want to browse, for example panasonic viera. This is on how I search the term in database:

P         


        
4条回答
  •  星月不相逢
    2021-01-26 06:47

    One solution would be to break up your query into individual terms and build a set of database queries connected by OR.

    terms = params[:q].split
    query = terms.map { |term| "name like '%#{term}%'" }.join(" OR ")
    Product.where(query).order('price')
    

提交回复
热议问题