【问题标题】:Getting undefined method `add_role' with Rails 6, Rolify using UUID使用 Rails 6 获取未定义的方法“add_role”,使用 UUID 进行 Rolify
【发布时间】:2019-12-13 16:27:52
【问题描述】:

我刚刚将 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

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

【问题讨论】:

    标签: ruby-on-rails uuid rolify


    【解决方案1】:

    您的问题是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)  
    

    find_by 将通过选择条件为真的第一条记录返回该类的实例(或nil 没有找到)。由于uuid 是独一无二的,这应该不是问题。

    【讨论】:

    • 太棒了!当然。我做了一个 Person.where... 来填充 p。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2020-04-11
    • 1970-01-01
    • 2013-01-20
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多