基本的なユーザー認証を設定しようとしています-ユーザーのログイン情報を並べ替えて、ユーザーのロールを追加する準備ができています。
基本的に、ユーザーに多くの役割を持たせて、ユーザーに権利へのアクセスを許可したいと思います。
シードデータをいくつか書きましたが、エラーが発生し続けます:
rake aborted!
undefined method `roles' for #<Array:0x007f8c0581ba80>
私のシードデータは次のようになります:
#user
user = User.create!([{ email: 'admin@admin.com', first_name: 'Admin', last_name: 'Test', password: 'admin', password_confirmation: 'admin'}])
user.roles << admins = Role.create!(:name => "Admin")
#user roles
create = Right.create!(:resource => "users", :operation => "CREATE")
read = Right.create!(:resource => "users", :operation => "READ")
update = Right.create!(:resource => "users", :operation => "UPDATE")
delete = Right.create!(:resource => "users", :operation => "DELETE")
#add the roles to the admin
admins.rights << read
admins.rights << create
admins.rights << update
admins.rights << delete
rake db:migrateは正常に機能し、すべてのテーブル列は期待どおりです。rake db:seedを実行すると、上記のエラーで異常終了します。エラーが何を言っているのか理解しています-has_manyをロールに定義していない場所がわかりません。
私はモデルを非常に綿密に調べましたが、私が見逃したものを見つけることができないようです。
私のモデルファイルは次のようになります。
class User < ActiveRecord::Base
has_secure_password
has_many :assignments
has_many :roles, :through => :assignments
attr_accessible :email, :first_name, :last_name, :password, :password_confirmation
validates_presence_of :email, :on => :create
validates :password, :confirmation => true
validates :password_confirmation, :presence => true
validates_uniqueness_of :email
#will be using this later to check if the user has access to resources/actions
# def can?(action, resource)
# roles.includes(:rights).for(action, resource).any?
# end
end
class Role < ActiveRecord::Base
has_many :grants
has_many :assignments
has_many :users, :through => :assignments
has_many :rights, :through => :grants
scope :for, lambda{|action, resource|
where("rights.operation = ? AND rights.resource = ?",
Right::OPERATION_MAPPINGS[action], resource
)
}
end
class Right < ActiveRecord::Base
attr_accessible :operation, :resource
has_many :grants
has_many :roles, :through => :grants
OPERATION_MAPPINGS = {
"new" => "CREATE",
"create" => "CREATE",
"edit" => "UPDATE",
"update" => "UPDATE",
"destroy" => "DELETE",
"show" => "READ",
"index" => "READ"
}
end
class Grant < ActiveRecord::Base
attr_accessible :right_id, :role_id
belongs_to :role
belongs_to :right
end
class Assignment < ActiveRecord::Base
belongs_to :user
belongs_to :role
attr_accessible :role_id, :user_id
end
どんな助けでも大歓迎です。