in custom advanced search form, best way perform query filter results?
class userscontroller < applicationcontroller def index @users = user.all end end
in many case have different types of data, example:
name
string, surname
string, age
integer
in specific case, have form filters user can or can not fill. how can dynamic query?
in controller this:
class userscontroller < applicationcontroller def index @users = user.search params[:filter] end end
then in user
model do:
class user < activerecord::base def self.search(options = {}) users = user.all users = users.where(name: options[:name] ) if options[:name].present? users = users.where(first_name: options[:first_name] ) if options[:first_name].present? users = users.where(last_name: options[:last_name] ) if options[:last_name].present? users = users.where(age: options[:age] ) if options[:age].present? users end end
Comments
Post a Comment