简体   繁体   English

Rails 3.2.9搜索多个条件

[英]Rails 3.2.9 search with multiple conditions

I need to build a search with multiple conditions. 我需要建立具有多个条件的搜索。 Two of the main ones are the following: City and State and Zip code. 以下是两个主要的代码:城市和州以及邮政编码。 In turn, I have other boolean attributes such as has_transportation, available_for_work, etc. The problem I face is I want to make my search flexible. 反过来,我还有其他布尔属性,例如has_transportation,available_for_work等。我面临的问题是我想使搜索更加灵活。 Meaning, for the boolean attribute, a user can select Either. 意思是,对于布尔属性,用户可以选择“任意”。 There is six boolean attributes in total along with searching based on zip or city and state. 总共有六个布尔属性以及基于邮政编码或城市和州的搜索。

In my mind I am thinking multiple conditional statements, but this doesn't seem like the Rails way or most effective way at accomplishing this search. 在我看来,我正在考虑多个条件语句,但这似乎不是Rails方式或完成此搜索的最有效方式。 Can someone please offer advice on how to solve my problem? 有人可以提供有关如何解决我的问题的建议吗?

Update: 更新:

Sample Scheme 样品方案

t.integer  "zip_code",               :null => false
t.boolean  "availability",           :default => true
t.integer  "travel_distance",        :default => 0
t.boolean  "transportation",         :default => false
t.boolean  "insurance",              :default => false
t.boolean  "tools",                  :default => false
t.boolean  "employee",               :default => false
t.boolean  "subcontractor",          :default => false
t.string   "city"
t.string   "state"

Output: The result returned would be zero or many accounts 输出:返回的结果将为零或多个帐户

Here's an example of what I am going to use: 这是我将要使用的示例:

 scope :tools, lambda { |arg| arg.blank? ? where("tools = ? OR tools = ?", true, false) : where(:tools => arg)}

Take a look at Ransack and it's demo app . 看看Ransack及其演示应用程序

Ransack will help you with building your search form and the query to run against the model. Ransack将帮助您构建搜索表单和针对模型运行的查询。

Since you're question is lacking lots of detail, my answer must too. 由于您的问题缺少很多细节,因此我的回答也必须如此。

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

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