我有一个名为Referral请求的模型以及相关的表和视图。我使用枚举定义了三个用户角色:
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发布于 2017-10-10 18:13:17
我忘了在Referral Requests中更改我的索引操作。这解决了这个问题。详细信息。
def index
@referral_requests = policy_scope(ReferralRequest)
endhttps://stackoverflow.com/questions/46670033
复制相似问题