Search code examples
ruby-on-railsrubyrubygemshas-scope

Rails - How can I use has_scope with collection_select?


I'm new to rails and I'm using the has_scope gem to help me filter a list to display as a table. I have the has_scope gem setup fine and working for basic parameters (search string, etc.)

How can I get has_scope to recognize the parameters passed in from a collection_select?

I'm doing...

<%= collection_select(:param_val, "company_name", Food.all, :company_name, :company_name, {}, {:multiple => true}) %>

Which passes the URL as

http://localhost:3000/foods?utf8=%E2%9C%93&param_val%5Bcompany_name%5D=McDonalds&commit=Search

And it doesn't filter, just returns the whole list. If I manually adjust the URL to...

http://localhost:3000/foods?utf8=%E2%9C%93&company_name%5D=McDonalds&commit=Search

Then it works fine. So I need to pass ONLY the company_name value, not the entire hash name, but I'm struggling on what I need to change.


Solution

  • For anyone else who runs into this issue, I wasn't able to solve it using the has_scope gem. In my controller I just did this instead...

      if  @company_filter_list.any?
        @filtered_foods = @all_foods.companies(@company_filter_list)  
      end