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.