Search code examples
ruby-on-railsransack

Rails show uniq results in dropdown


I have a database table payments with a column PAYMENT that can be cash, card etc..

In my payments index view I want to add a filter to filter results by payment type.

The best I came up so far is:

<%= f.select :PAYMENT_eq, options_from_collection_for_select(Payment.uniq, :ID, :PAYMENT, @q.PAYMENT_eq), {:include_blank => true}, {class: 'myselect'} %>

which shows a dropdown with payment types. The problem is that it returns an option for each row. For instance if I have 10 payments with payment type cash, in dropdown I see 10 options cash.

The syntax I used for that filter is the same I use for filter on asssociations, while this is not. So perhaps I'm over complicating, but I couldn't think of any other solution.

I tried Payment.uniq but it doesn't work.

How can I fix it?


Solution

  • Try plucking the payments and apply .uniq on them

    If you have duplicates in the Payment model don't pluck :id or select the payments from another table if you have PaymentType or PaymentMethod

    <%= f.select :PAYMENT_eq, options_for_select(Payment.pluck(:PAYMENT).uniq, @q.PAYMENT_eq), {:include_blank => true}, {class: 'myselect'} %>