简体   繁体   中英

Rails: combining optional params into a query

I have a view with a huge paginated list of records that need filtering.

Users can filter by records a few different ways (such as 'saved' records, 'read' records, and 'mark deleted' records) and I'd like for them to be able to these filters any possible way. 这些过滤器。

My current, flawed, non-functioning approach. The code below does not produce anything unless all of the params are specified and valid:

#view. Set the 'se' filter to true; leave all others as is 
<%= link_to list_url(:id=>params[:id], :se=>"true", :st=>params[:st], :re=>params[:re]) do %> 
  <div class="button">Toggle SE</div>
<% end %>

#controller query. Add whichever params are passed into the conditions for the new page.
#query is paginated and sorted
@records = Record.where("user_id IN (?) AND see = ? AND star = ? AND delete = ? AND like = ?", @users.select("id"), params[:se], params[:st], params[:re]).paginate :page => params[:page], :order => (sort_column + " " + sort_direction)

What is the best way to create this filtering system?
I imagine a client-side sort would be faster than asking the server to become involved every time - is there a simple AJAX way to accomplish this kind of thing? Imagine filters the user can in any combination. 过滤器。

Try this:

conditions = {:user_id => @users.select("id")}
{
 :se => :see,
 :st => :star,
 :del => :delete
}.each{|k1, k2| conditions[k2] = params[k1] unless params[k1].blank?}

@records = Record.where(conditions).paginate(...)

The conditions hash will be filled based on the values present in the params hash.

Edit 1

You can combine conditions hash and array.

@records = Record.where(conditions).where(
  ":created_at > ?",  Date.today - 30).paginate(...)

You can change the user_id condition to what ever you want by specifying

conditions[:user_id] = @user.id

In the above statement, if the RHS is an array, rails automatically generates the IN clause. Otherwise, equality check( = ) is performed.

也可以使用匿名作用域: 在Rails中组合条件数组

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