简体   繁体   中英

Rails 4.2 enum not working

I am trying to use rails enum fields for the user status functionality in my app however enums don't seem to be working(I have used Devise to manage the user login, registration .etc). this is the result in my rails console where the status appears as status:0 instead of status:pending status:active status:suspended .etc

  user = User.first   User Load (1.6ms)  SELECT  "users".* FROM "users"  ORDER BY "users"."id" ASC LIMIT 1
 => #<User id: 1, email: "email@gmail.com", encrypted_password:
 "$2a$11$TFSX2XkearxEz1JlRtd6hOcvj3ScqykNKEKrkqGYLDx...",
 reset_password_token: nil, reset_password_sent_at: nil,
 remember_created_at: nil, sign_in_count: 1, current_sign_in_at:
 "2016-06-10 22:57:03", last_sign_in_at: "2016-06-10 22:57:03",
 current_sign_in_ip: "::1", last_sign_in_ip: "::1", created_at:
 "2016-06-10 22:57:03", updated_at: "2016-06-11 10:59:41", first_name:
 "John", last_name: "Smith", phone: nil, **status: 0**>

but I have declared the status enum in my user.rb

  class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  enum status: [:pending, :active, :suspended]
end

and here is my migration

class AddStatusColumn < ActiveRecord::Migration
  def change    
    add_column :users, :status, :integer, default: 0    
  end
end

Furthermore, when I use user.pending? (default) I get an error "undefined method `pending?'" for instance

    irb(main):014:0> user.pending?
NoMethodError: undefined method `pending?' for #<User:0x315dba8>
        from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/activemodel-4.2.6/lib/active_model/attribute_methods.rb:433:in `method_missing'
        from (irb):14
        from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.6/lib/rails/commands/console.rb:110:in `start'
        from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.6/lib/rails/commands/console.rb:9:in `start'
        from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:68:in `console'
        from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
        from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.6/lib/rails/commands.rb:17:in `<top (required)>'
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>'

how do I fix this issue with rails enum? Thanks!

You need to reload or restart the Rails console when you change code in the application.

Depending on the type of change the faster reload might solve your problem:

> reload!

On bigger changes: When you added or removed gems or change code outside of the app folder (for example a initializer or something in the lib folder) you will need to exit and restart the console completely:

> exit
$ rails console

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM