Search code examples
ruby-on-railssimple-form

How to list a ruby on rails enum attributes?


I have a class Foo with an enum status:

Class Foo  < ApplicationRecord
   enum status: [:not_loaded, :loaded, :stale]
end

How do I access the list of status attributes? For example using simple_form I want to do

<%= f.input :status, collection: list_of_statuses %>

Solution

  • If you do Foo.statuses it returns { :not_loaded => 0, :loaded => 1, :stale: 2 }, so you can get list the attributes with Foo.statuses.keys. So the simple_form declaration would be

    <%= f.input :status, collection: DataSet.statuses.keys %>