1

I have an app with devise. I installed the role_model and cancan gems. I updated the user.rb to read:

require 'rubygems'
require 'role_model'

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :email, :password, :password_confirmation, :remember_me, :roles, :roles_mask

  include RoleModel

  roles_attribute :roles_mask

  roles :admin, :contributor, :guest
end

Then, in the rails console I try

> u = User.find(:first)
  User Load (1.2ms)  SELECT "users".* FROM "users" LIMIT 1
 => #<User id: ...
> u.roles = [:admin]
NoMethodError: undefined method `roles_mask=' for #<User:0x00000002b57500>
    from /home/xxx/.rvm/gems/ruby-1.9.3-p194@rails-3.2.11/gems/activemodel-3.2.11/lib/active_model/attribute_methods.rb:407:in `method_missing'

Where does roles_mask come from? Do I need to include something else?

4

2 に答える 2

7

この移行のように、gemのインストールに加えて、列をテーブルにroles_mask追加する必要がありますusers

class AddRolesMaskToUser < ActiveRecord::Migration
  def change
    add_column :users, :roles_mask, :integer
  end
end

これで問題は解決します。

于 2013-01-25T21:31:55.723 に答える