0
我有一個模型叫做推薦請求和相關的表和視圖。我有三個使用枚舉定義的用戶角色:評論政策範圍沒有正確實施
enum role: { staff: 0, clinician: 1, admin: 2 }
職員用戶屬於大學,而大學有很多職員用戶。我在許多應用程序中的意圖是使用權威政策來顯示與大學其他用戶相關聯的員工用戶記錄。例如,我正在嘗試爲引薦請求做這件事,但我有一些配置不正確,因爲它顯示任何給定用戶的所有引薦請求,而不管它們是否由屬於其大學的另一用戶創建。我究竟做錯了什麼?
轉診請求策略:
class ReferralRequestPolicy < ApplicationPolicy
class Scope
attr_reader :user, :scope
def initialize(user, scope)
@user = user
@scope = scope
end
def resolve
if user.admin?
scope.all
else
scope.joins(:user).merge(User.where(university: user.university))
end
end
end
def index?
user.staff? or user.admin?
end
end
轉診請求型號:
class ReferralRequest < ApplicationRecord
belongs_to :user, -> { where role: :staff }
belongs_to :patient
has_many :dispatches
has_many :clinicians, through: :dispatches
has_and_belongs_to_many :languages
has_and_belongs_to_many :races
has_and_belongs_to_many :genders
validates :user_id, presence: true
enum status: { created: 0, sent: 1, shared: 2, closed_under_care: 3, closed_not_seeking_care: 4, closed_unresponsive: 5 }
end
人員用戶關注:
require 'active_support/concern'
module StaffUser
extend ActiveSupport::Concern
included do
belongs_to :university
has_many :patients
has_many :referral_requests
validates :university_id, presence: true, if: :staff?
end
class_methods do
end
end
大學模式
class University < ApplicationRecord
has_many :staffs, -> { where role: :staff}, class_name: "User"
has_many :clinicians, through: :lists
has_many :whitelists
belongs_to :market
validates :market_id, presence: true
end