I've got a table, 'jobs' with a enum field 'status'. status has the following enum set:
enum status: [ :draft, :active, :archived ]
using ransack, how do I filter the table for, say, all active records?
You can declare own ransacker in model like this:
ransacker :status, formatter: proc {|v| statuses[v]} do |parent|
parent.table[:status]
end
Then You can use default ransack syntax _eq
to check equality like this:
Model.ransack(status_eq: 'active').result
Edit: If column name doesn't change you can skip block of code:
ransacker :status, formatter: proc {|v| statuses[v]}
This is something I use in my views for enums and ransack:
<%= f.select :status_eq, Model.statuses.to_a.map { |w| [w[0].humanize, w[1]] },
{:include_blank => true} %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With