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

Getting undefined method `add_role' with Rails 6, Rolify using UUID

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

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

2.6.2 :002 > p.add_role :admin

回溯(最近调用最后): 1: 来自 (irb):2 NoMethodError(#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

如有任何帮助,我们将不胜感激!

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

如果您希望所有这些人都拥有管理员角色,那么

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

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

p = Person.where(uuid: some_uuid) 

然后将其更改为

p = Person.find_by(uuid: some_uuid) # or Person.find_by_uuid(some_uuid)  

find_by 将 return class 的实例(或找到 nil none),方法是选择条件为真的第一条记录.由于 uuid 是独一无二的,所以这应该不是问题。