简体   繁体   中英

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. 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. 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 will help you with building your search form and the query to run against the model.

Since you're question is lacking lots of detail, my answer must too.

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