Search code examples
rubyruby-on-rails-3ruby-on-rails-4ransack

Ransack on enum filter for dropdown list


class Schedule < ApplicationRecord 
  belongs_to :event
  enum county: {USA: 0, INDIA: 1, Brasil: 2}
end

ransack select in index.erb.html

<%= f.collection_select :schedules_county_matches_all, Schedule.counties.map{ |dp| [dp.first, dp.first.humanize] }, :first.to_s, :second ,:include_blank => "All"%>

which gives output select dropdown list but not value as in Postgres database the value stored as integer not string

<li class=""><span>All</span></li>
<li class=""><span>USA</span></li>
.......

As working filter, I guess in view it should be added value in li

<li value=""><span>All</span></li>
<li value="0"><span>USA</span></li>
.......

Solution

  • Use below line code for your problem

    <%= f.select :schedules_county_matches_all, Schedule.counties.map { |r| [r[0], r[1].to_i] }, include_blank: true %>