【发布时间】: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