使用Rails 6获取未定义的方法'add_role',使用UUID进行Rolify

问题描述 投票:0回答:1

我刚刚将rolify添加到我的Rails 6应用程序中,该应用程序对所有表都使用UUID。

在出现最初错误之后,我发现我need to change my migrations slightly正在处理UUID。我还在使用一个名为“ Person”的模型,而不是默认的“ User”。我已经尝试重启服务器(几次),但是仍然得到以下信息:

2.6.2 :002 > p.add_role :admin

Traceback(最近通话):1:来自(irb):2NoMethodError(#Person :: ActiveRecord_Relation:0x00007fe37ec29408的未定义方法`add_role')2.6.2:003>

以下是适用的型号:

role.rb

class Role < ApplicationRecord
    has_and_belongs_to_many :people, :join_table => :people_roles


belongs_to :resource,
           :polymorphic => true,
           :optional => true


validates :resource_type,
          :inclusion => { :in => Rolify.resource_types },
          :allow_nil => true

scopify
end

person.rb

class Person < ApplicationRecord
rolify

def full_name
    "#{self.last_name}, #{self.first_name}"
end
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable, :trackable
end

适用模式:

create_table "people", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "last_name"
t.string "first_name"
t.string "gender"
t.uuid "personable_id"
t.string "personable_type"
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.integer "failed_attempts", default: 10, null: false
t.string "unlock_token"
t.datetime "locked_at"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "type"
t.index ["email"], name: "index_people_on_email", unique: true
t.index ["reset_password_token"], name: "index_people_on_reset_password_token", unique: true
t.index ["unlock_token"], name: "index_people_on_unlock_token", unique: true
 end

create_table "people_roles", id: false, force: :cascade do |t|
    t.uuid "person_id"
    t.uuid "role_id"
    t.index ["person_id", "role_id"], name: "index_people_roles_on_person_id_and_role_id"
  end

create_table "roles", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
    t.string "name"
    t.uuid "resource_id"
    t.string "resource_type"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.index ["name", "resource_type", "resource_id"], name: "index_roles_on_name_and_resource_type_and_resource_id"
    t.index ["name"], name: "index_roles_on_name"
  end

对此有任何帮助,将不胜感激!

ruby-on-rails uuid rolify
1个回答
0
投票

您的问题是p不是Person,而是Person所指示的ActiveRecord_Relation对象的集合。

[如果您希望所有这些人都具有管理员角色,那就应该是

p.each {|person| person.add_role :admin } 

否则,您将需要更改填充p的代码以返回单个Person对象。例如,如果您将p填充为

p = Person.where(uuid: some_uuid) 

然后将其更改为

p = Person.find_by(uuid: some_uuid) # or Person.find_by_uuid(some_uuid)  
© www.soinside.com 2019 - 2024. All rights reserved.