Rails 搜索 - 多个特定模型属性选择器 [英] Rails search - multiple specific model attributes selector
问题描述
我有一个包含一堆属性的事件"数据库.如何允许我的用户搜索事件".我看过有关进行文本搜索的教程,但我正在寻找更多的 ON/OFF 搜索.
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
必须包含多个属性和布尔数学(是事件"位置 5 公里范围内的搜索位置.
Must include multiple attributes and boolean math (is the search location within 5km of an 'event' location.
谢谢
推荐答案
尝试 范围.为模型中的每个可搜索属性创建一个范围.
例如时间属性:scope :time_search, ->(min, max) { where("time_column > ? AND time_column ", min, max) }
然后从控制器:Model.time_search(params[:min], params[:max])
.
您还可以像这样链接作用域:Model.scope_1(param).scope_2(param)
,它将根据这两个作用域中的条件过滤记录.
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.
这篇关于Rails 搜索 - 多个特定模型属性选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!