Search code examples
rubyransack

Searching on an Enum Field with Ransack


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?


Solution

  • 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]}