Rails, filterrific with multiple parameters

1.4k views Asked by At

I'm using the most recent ruby + rails, with the filterrific gem. It works great - but how can multiple parameters per scope be used? For single column filtering it is simple, but for the following scenario, how would it be handled?

Filter by X miles of zipcode Y

Scopes currently only have knowledge of the value being modified (EITHER miles, OR zipcode - two different scopes), but neither have knowledge of the other. This filter scenario requires knowledge of miles and zipcode. I have searched the documentation thoroughly and see no way. Has anyone done this before or am I missing something hidden in the documentation?

1

There are 1 answers

0
Jo Hund On BEST ANSWER

You can use Rails' fields_for form helper to submit a hash with multiple keys and values to a single filterrific enabled scope:

<%= f.fields_for :with_distance do |with_distance_fields| %>
  <%= with_distance_fields.text_field :max_distance %>
  <%= with_distance_fields.text_field :city %>
<% end %>

Then you can access the various values in the scope like so:

scope :with_distance, lambda { |distance_attrs|
  # `distance_attrs` is a hash with two keys:
  # {
  #   :max_distance => '10',
  #   :city => 'Vancouver',
  # }
  where(...)
}

This is tested with Filterrific 1.4.2 and Rails 4.1