I’m having issues seeding my database with the default roles of my users.
class User < ActiveRecord::Base
attr_accessible :email, :password, :password_confirmation, :remember_me, :username
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
validates_presence_of :username, :email, :password, :password_confirmation
validates_uniqueness_of :username, :email
before_create :setup_default_role_for_new_users
ROLES = %w[admin default]
private
def setup_default_role_for_new_users
self.roles = [ "default" ]
end
end
Seeds.rb
puts 'Loading seed data now....'
user1 = User.create(:username => 'admin', :email => 'testing@email.com', :password => 'qweasd',:password_confirmation => 'qweasd', :role => 'admin')
user2 = User.create(:username => 'userone', :email => 'user1@email.com', :password => 'qweasd', :password_confirmation => 'qweasd')
user3 = User.create(:username => 'usertwo', :email => 'user2@email.com', :password => 'qweasd',:password_confirmation => 'qweasd')
puts 'Users added'
When seeding the error I get:
undefined method `roles' for #<User:0x53f4c60>
I have have the role column in my database so why wouldn’t it work?
Add :roles to your attr_accessible if you want to be able to mass assign it like in:
also is it
:roleor:roles? Check your database, if you have:roleor:rolespaste your schema.db here.