简体   繁体   English

Rails搜索-多个特定模型属性选择器

[英]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. 我看过有关进行文本搜索的教程,但是我正在寻找更多的ON / OFF搜索。

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. 必须包含多个属性和布尔数学(是在“事件”位置5公里之内的搜索位置。

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]) . 然后从控制器: 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. 您也可以像这样链接作用域: Model.scope_1(param).scope_2(param) ,它将根据来自这两个作用域的条件过滤记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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