【问题标题】:ruby on rails rake db:seed undefined method forruby on rails rake db:seed 未定义的方法
【发布时间】:2012-05-13 18:45:39
【问题描述】:

我正在尝试设置基本用户身份验证 - 我已对用户登录内容进行了排序,并准备为用户添加角色。

基本上我希望我的用户拥有许多角色,这使他们能够访问权限。

我写了一些种子数据,但一直报错:

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

任何帮助将不胜感激。

【问题讨论】:

    标签: ruby-on-rails ruby rake seed migrate


    【解决方案1】:

    只需去掉第一行中的[]{},例如:

     user = User.create!(email: 'admin@admin.com', first_name: 'Admin', last_name: 'Test', password: 'admin', password_confirmation: 'admin')
    

    【讨论】:

      【解决方案2】:

      您不应将一个用户创建为用户数组。尝试删除 User.create!() 中的方括号

      user = User.create!({email: 'admin@admin.com', first_name: 'Admin', last_name: 'Test', password: 'admin', password_confirmation: 'admin'})
      

      【讨论】:

        猜你喜欢
        • 2017-10-18
        • 2014-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-21
        • 1970-01-01
        • 2016-12-15
        • 2019-10-31
        相关资源
        最近更新 更多