Problem with changing value of field iin devise table

13 views
Skip to first unread message

Tsvetelina Borisova

unread,
May 29, 2012, 5:35:27 PM5/29/12
to plataforma...@googlegroups.com
I have table - users that was created with scaffold but now it is used by devise.I have added a field - role and every time a user is created or updated I have to explicitly say: user.update_attribute(:role,"admin"). Do you know why? Thanks in advance : )

Carlos Antonio da Silva

unread,
May 29, 2012, 6:26:33 PM5/29/12
to plataforma...@googlegroups.com
I definitely have no idea with this little info, please send models code, example of what's wrong and how it should work, otherwise it's hard to help.

-- 
At.
Carlos Antonio

Tsvetelina Borisova

unread,
May 30, 2012, 2:43:18 AM5/30/12
to plataforma...@googlegroups.com

Thank you for responding :)

Here is what I have in user.rb:
class User < ActiveRecord::Base

  has_many :instances 
  # Include default devise modules. Others available are:
  # :token_authenticatable,  :lockable and :timeoutable
  devise :database_authenticatable,:confirmable,
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me 
end

And here is my migration:

  def self.up
    add_column :users, :role, :string, :default=>"user"
  end

  def self.down
    remove_column :users, :role
  end

And here is what I have in schema.rb
  create_table "users", :force => true do |t|
    t.string   "email",                               :default => "",            :null => false
    t.string   "encrypted_password",   :limit => 128, :default => "",            :null => false
    t.string   "password_salt",                       :default => "",            :null => false
    t.string   "reset_password_token"
    t.string   "remember_token"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",                       :default => 0
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "confirmation_token"
    t.datetime "confirmed_at"
    t.datetime "confirmation_sent_at"
    t.string   "role",                                :default => "user"
  end

  add_index "users", ["email"], :name => "index_users_on_email", :unique => true
  add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true

Again - thanks for the quick response :)

Carlos Antonio da Silva

unread,
May 30, 2012, 7:49:19 AM5/30/12
to plataforma...@googlegroups.com
Ok, so what's the deal with "user.update_attribute(:role,"admin")"?

Your default for this attribute is "user", when a new user is created, it gets "user" role by default. If you want to change to admin, you have to set it. I think I didn't get what's the issue you're having yet :)

-- 
At.
Carlos Antonio

Tsvetelina Borisova

unread,
May 30, 2012, 8:23:54 AM5/30/12
to plataforma...@googlegroups.com
I found that I haven't added attr_accessible :role to my User model. Silly mistake :) Thanks again for answering so quickly : )
Reply all
Reply to author
Forward
0 new messages