Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Properly Configure Rails 4.1 Enums in ActiveAdmin

I've got a Rails 4.1 app in which I use an enum to represent the privacy level of an object.

In my schema:

t.integer "privacy_level", default: 0

In my model:

enum privacy_level: { privacy_private: 0, privacy_trusted: 1, privacy_public: 2 }

In my ActiveAdmin register file:

index do
  column :privacy_level
  default_actions
end

form do |f|
  f.inputs "Edit My Model" do
    f.input :privacy_level
  end
  f.actions
end

On the ActiveAdmin index page, it works great. The privacy level of each object shows up as "privacy_private", "privacy_trusted", and "privacy_public".

However, when I try to edit an object, the input type is a number box with up and down arrows which allow me to put any integer in, regardless of whether or not the integer is a valid privacy level (even negative values).

What I would like to see is a dropdown (select) input with the three enumerated string values I defined in my model.

like image 463
Pop-A-Stash Avatar asked May 01 '14 19:05

Pop-A-Stash


People also ask

How to set enum value Rails?

Instead, we can add prefix and suffix as per our requirement and call the methods accordingly. # app/models/post. rb class Post < ActiveRecord::Base enum :status, { draft: 0, published: 1, archived: 2, trashed: 3 }, prefix: true enum :category, { free: 0, premium: 1 }, suffix: true end Post. free_category post.

How to add enum column Rails migration?

First of all, you need to create an appropriate migration. Notice that column type is set to integer and this is how Rails keeps enums values in the database. Next step is to declare enum attribute in the model. Run the migrations and that's it!

What is active admin?

Active Admin is a Ruby on Rails plugin for generating administration style interfaces. It abstracts common business application patterns to make it simple for developers to implement beautiful and elegant interfaces with very little effort.

What is an enum Ruby?

Ruby on Rail's ships with a module known as Enum which has a parent class of ActiveRecord . This handy module allows you to declare different states in the database using any Model in your Rails application. Enums are powerful thanks to the built-in methods and scopes that come with the framework.


3 Answers

Building off of Jack's answer, here's what worked for me. Say your ActiveRecord model is Tweets:

f.input :privacy_level, as: :select, collection: Tweet.privacy_levels.keys

Key things to note here:

  • your ActiveRecord has a useful dictionary (available at enum_name.pluralize) of enum keys to values.
  • using strings (and ignoring the underlying integer representation) makes it easier to write to the enum value.
like image 113
AlexeyMK Avatar answered Oct 22 '22 17:10

AlexeyMK


In order to use enums in ActiveAdmin's filters use:

filter :level, as: :select, collection: Model.levels

assuming an enum attribute named level

This will make sure to actually put the integer value in the query and not the key name.

like image 28
ChrHansen Avatar answered Oct 22 '22 18:10

ChrHansen


do this:

f.input :privacy_level, :as => :select, :collection =>  privacy_level.keys.to_a
like image 7
jimagic Avatar answered Oct 22 '22 19:10

jimagic