W
W
wimanen2015-12-23 11:14:05
Ruby on Rails
wimanen, 2015-12-23 11:14:05

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

I would be very grateful for advice on simplifying the selection or a direction where to google, thanks in advance

Answer the question

In order to leave comments, you need to log in

2 answer(s)

guides.rubyonrails.org/active_record_querying.html...

I
Ilya Lozer, 2015-12-23
@ammet

https://github.com/activerecord-hackery/ransack

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question