简体   繁体   中英

Rails search - multiple specific model attributes selector

I have a database of 'events' with a bunch of attributes. How can I allow my users to search for 'events'. I have seen the tutorial on doing text searches, but I'm looking for more ON/OFF searches.

Each event has a time, location (coordinates), and a few other selectors. I would like a user to be able to search based on time (min,max) and/or location (closest to their input coordinates) and select from selectors.

How can I do this? Any specific tutorials that I should look at. I couldn't find anything near by nears. Thanks

Must include multiple attributes and boolean math (is the search location within 5km of an 'event' location.

Thanks

Try scopes . Create a scope for each searchable attribute in a model.
For example time attribute:

scope :time_search, ->(min, max) { where("time_column > ? AND time_column < ?", min, max) }

And then from controller: Model.time_search(params[:min], params[:max]) .
You can also chain scopes, like this: Model.scope_1(param).scope_2(param) , which will filter records based on criteria from those 2 scopes.

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