简体   繁体   中英

active record contains query

how can I implement "contains function" in active records? I am trying to query my database which contains the following data as an example

brand: ipad model : ipad 3gen specs : wifi+3g 32 GB black

when the user searched for "ipad black 3gen" he should get the above record.

this is my trail which didnt work

    @products = Product.where("brand like ? OR generation like ? OR spec_overview like ? ", "%"+params[:query].capitalize+"%", "%"+params[:query].capitalize+"%", "%"+params[:query].capitalize+"%")

Sql is not a great fit for this type of query. You can do it with a fulltext index probably, but using something like Solr would be much less painful, and more accurate. You can integrate Solr into a Rails app with sunspot if you decide to go that direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM