ruby - Rails 4.2 enum not working -
i trying use rails enum fields user status functionality in app enums don't seem working(i have used devise manage user login, registration .etc). result in rails console status appears status:0
instead of status:pending
status:active
status:suspended
.etc
user = user.first user load (1.6ms) select "users".* "users" order "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 have declared status enum in user.rb
class user < activerecord::base # include default devise modules. others available are: # :confirmable, :lockable, :timeoutable , :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable enum status: [:pending, :active, :suspended] end
and here migration
class addstatuscolumn < activerecord::migration def change add_column :users, :status, :integer, default: 0 end end
furthermore, when use user.pending?
(default) error "undefined method `pending?'" instance
irb(main):014:0> user.pending? nomethoderror: undefined method `pending?' #<user:0x315dba8> 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' (irb):14 c:/railsinstaller/ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.6/lib/rails/commands/console.rb:110:in `start' c:/railsinstaller/ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.6/lib/rails/commands/console.rb:9:in `start' 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' 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!' c:/railsinstaller/ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.6/lib/rails/commands.rb:17:in `<top (required)>' bin/rails:4:in `require' bin/rails:4:in `<main>'
how fix issue rails enum? thanks!
you need reload or restart rails console when change code in application.
depending on type of change faster reload might solve problem:
> reload!
on bigger changes: when added or removed gems or change code outside of app
folder (for example initializer or in lib
folder) need exit , restart console completely:
> exit $ rails console
Comments
Post a Comment