etlds
etlds

Reputation: 5890

rails 4.1 unable to get all enum types

I just upgrade my app to rails 4.1.0.beta1

I have a class

class User < ActiveRecord::Base
    enum usertype: { :employee => 10, :boss => 30, :manager => 40, :admin => 50 }
}

All the enum feature works well like user.boss? # ture

But when I try to get all user types by

User.usertypes

I got a undefined method for "usertypes"

Any helps?

This is the link the I learn from http://edgeapi.rubyonrails.org/classes/ActiveRecord/Enum.html

Edit: migration

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :email
      t.string :password_digest
      t.string :remember_token
      t.boolean :is_locked
      t.integer :usertype, default: 10
      t.timestamp :last_login_at

      t.timestamps
    end
  end
end

Upvotes: 3

Views: 1870

Answers (1)

Helios de Guerra
Helios de Guerra

Reputation: 3475

Have you created the proper migrations?

Enum attributes are values which map to integers in the database, but can be queried by name.

So you'll need a migration which adds an integer column with the name 'usertype' to the Users table. Have you done that?

EDIT 1:

Okay, just tested it out and the class method to get the mapping doesn't work with Rails 4.1.0.beta1, but does work with edge Rails. So hopefully that gets pulled into the next beta release...

EDIT 2:

Also, check out this commit which demonstrates the way you'd access the enum mapping in Rails 4.1.0.beta1 using a constant instead of a class method. So in your case you'd use User::USERTYPE to access your mapping.

Upvotes: 3

Related Questions