I am trying to create a Ransack section for our search page that will look across a student's grade table and find students within a range for each kind of grade.
Given: Student has_many Grades and Grade belongs_to Category
I want a form section like: Biology Grade grater than [ 2 ] Math Grade grater than [ 3 ]
Eventually something like: SELECT... WHERE (grade.value >= 2 AND grade.category_id = 1) AND (grade.value >= 3 AND grade.category_id = 2)
Any ideas on how to set that up?
You need to build condition groupings. Take a look at https://github.com/activerecord-hackery/ransack/blob/master/lib/ransack/helpers/form_builder.rb#L74 and follow the code. The Ransack demo application is also a good resource.
It's a shame that Ransack does not have a comprehensive documentation. I spent a whole day trying to make similar feature work.
Try something like this:
<%= hidden_field_tag 'q[c]', 'or' %>
<% Category.each_with_index do |category, i| %>
<%= hidden_field_tag "q[g][#{i}][m]", 'and' %>
<%= text_field_tag "q[g][#{i}][c][0]", 'grades_value_gteq' %>
<%= text_field_tag "q[g][#{i}][c][1]", 'grades_category_eq' %>
<% end %>
You are building groupings of conditions inside Ransack. Inside each you join the conditions with AND and then join the groups with OR.