Search Enum with Ransack

I have a table, 'jobs' with an enum 'status' field. status has the following set of enumerations:

enum status: [ :draft, :active, :archived ] 

using ransack, how do I filter a table, say, for all active records?

+5
source share
2 answers

This is what I use in my representations for enums and ransack:

 <%= f.select :status_eq, Model.statuses.to_a.map { |w| [w[0].humanize, w[1]] }, {:include_blank => true} %> 
+6
source

You can declare your own ransacker in the model as follows:

 ransacker :status, formatter: proc {|v| statuses[v]} do |parent| parent.table[:status] end 

You can then use the default _eq syntax _eq to _eq for equality as follows:

 Model.ransack(status_eq: 'active').result 

Edit: if the column name does not change, you can skip the code block:

 ransacker :status, formatter: proc {|v| statuses[v]} 
+5
source

All Articles