Answer the question
In order to leave comments, you need to log in
Is it possible to optimize (simplify) the set of conditions for filtering?
The controller has an Index method which. depending on whether the form was submitted for filtering, it displays records.
That is, on the user list page, there is a form that sends a request to the current controller using the GET method. And if the parameters for filtering were not passed, then all records are displayed. If they were transferred, then which ones and by them filters the records and displays the already filtered ones.
Is it possible to simplify the conditions in the filter method. given that there will be up to 11 filter options?
def index
@filteruser = User.new
cnf = nil
user = params[:user]
def filter(user)
@username = user[:name]
@userlastname = user[:lastname]
# Еще 9 параментров
@username&&@userlastname ? cnt = User.where(:name => @username, :lastname => @userlastname) : false
@username&&@userlastname.blank? ? cnt = User.where(:name => @username) : false
@username.blank? && @userlastname ? cnt = User.where(:lastname => @userlastname) : false
# Еще дохрена условий??
@users = cnt
end
user ? filter(user) : @users = User.all
@user_cnt = @users.count
end
Answer the question
In order to leave comments, you need to log in
guides.rubyonrails.org/active_record_querying.html...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question