ruby on rails - Dynamic find conditions in active record -
I have an index action in the train which can handle some parameters eg:
Parameters [: first_name] can be # zero or first_name parameter [: age] can be # zero or age parameter [: country] # can be zero or country
when users find I would like it and all the conditions which are not zero, it searches for 8 situations.
How can I keep my code dry and flexible and do not end up with a group of if
statement to create situations for search. If no conditions are specified, then I will return User.all
Want to
How about something like this:
Terms = params.only (: first_name ,: age, country) terms = conditions.delete_if {| Key, value | Value.blank?} If conditions.empty? User.All other User.all (: conditions => conditions) end
Comments
Post a Comment