2017-10-10 63 views
0

我有一個叫做dispatch的模型,它是一個連接表,它與另外兩個模型,引用請求和臨牀醫生相關。在我的應用程序中,工作人員用戶將創建一個轉診請求記錄,其中包含關於他們的病人正在尋找什麼的一些詳細信息。一些未構建的邏輯會查找匹配患者記錄中所述至少一項保險的所有臨牀醫生用戶,並且這將導致向這些臨牀醫生中的每一位發送郵件。調度模型是我如何嘗試記錄推薦請求與臨牀醫生之間的關係,因此我知道哪些臨牀醫生收到了電子郵件,並且我也可以記錄他們的答案(是或否)。使用專家策略來限制另一個類的對象範圍

臨牀醫生也屬於市場,員工屬於屬於市場的大學。

每位臨牀醫生都有臨牀醫師資料,除了clincian_profiles表格外,還有一個clinicalician_profiles_insurances表格,所以我可以給臨牀醫生指定的用戶打電話給.clinician_profile.insurances,並返回他們所有的保險。

根據給定的轉診請求,我還可以致電.insurances顯示患者所有的保險。

我試圖使用調度新方法內的權威人士政策範圍,做到以下幾點:誰是在同一市場爲員工用戶的大學

- 只顯示用戶的臨牀醫師。

- 僅顯示臨牀醫生用戶,他們至少有一項與轉診請求相匹配的保險。

這是我迄今爲止最好的滑動,它甚至不能限制市場的範圍。我究竟做錯了什麼?

調度策略

class DispatchPolicy < 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(:clinician).merge(User.where(market: user.market)) 
     end 
    end 
    end 

我的用戶有多個角色:

enum role: { staff: 0, clinician: 1, admin: 2 } 

這裏有關係:

class Dispatch < ApplicationRecord 
    belongs_to :referral_request 
    belongs_to :clinician, class_name: 'User', foreign_key: 'user_id' 

class ReferralRequest < ApplicationRecord 
    belongs_to :user, -> { where role: :staff } 
    has_many :dispatches 
    has_many :clinicians, through: :dispatches 

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 

這裏是我的在急件控制器的新方法:

def new 
@clinicians = policy_scope(Dispatch) 
@dispatch = @referral_request.dispatches.new 
end 

這裏是調度表/ new.html.erb這裏我試圖顯示誰

1)是臨牀醫生的所有用戶 2)屬於與工作人員用戶的大學相同的市場 3)在他們的臨牀醫師檔案中至少有一份保險符合與轉診請求相關的保險。

<% @clinicians.each do |clinician| %> 
    <tr> 
    <td><%= clinican.id %></td> 
    <td><%= clinican.name %></td> 
    <td><%= clinican.market.name %></td> 
    <td><%= clinican.clinician_profile.insurances.map(&:name).to_sentence %></td> 
    <td><%= clinican.clinician_profile.genders.map(&:name).to_sentence %></td> 
    <td><%= clinican.clinician_profile.races.map(&:name).to_sentence %></td> 
    <td><%= clinican.clinician_profile.languages.map(&:name).to_sentence %></td> 
    </tr> 
<% end %> 
</tbody> 
</table> 

保險模式:

class Insurance < ApplicationRecord 
    has_and_belongs_to_many :patients 
    has_and_belongs_to_many :clinician_profiles 
    has_and_belongs_to_many :markets 
end 

患者模型

class Patient < ApplicationRecord 
    belongs_to :author, -> { where role: :staff }, class_name: 'User', foreign_key: 'user_id' 

    has_and_belongs_to_many :genders 
    has_and_belongs_to_many :concerns 
    has_and_belongs_to_many :insurances 
    has_and_belongs_to_many :races 
    has_many :referral_requests 
    belongs_to :staff_doctor, class_name: 'User', foreign_key: 'staff_doctor_id' 

    def has_referral? 
    !self.referral_requests.empty? 
    end 
end 

**Clinician concern** 
require 'active_support/concern' 

module ClinicianUser 
    extend ActiveSupport::Concern 

    included do 
    has_one :clinician_profile 
    has_many :lists 
    has_many :universities, through: :lists 
    has_many :dispatches 
    has_many :referral_requests, through: :dispatches 
    after_create :create_clinician_profile 
    belongs_to :market 
    validates :market_id, presence: true, if: :clinician? 
    end 

    class_methods do 
    end 
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 ClinicianProfile < ApplicationRecord 
    belongs_to :user, -> { where role: :clinician } 
    has_and_belongs_to_many :languages 
    has_and_belongs_to_many :races 
    has_and_belongs_to_many :insurances 
    has_and_belongs_to_many :genders 
end 

上午我甚至在想這個正確的方式?對於我想要做的全部或部分事情來說,專家政策是一個很好的解決方案嗎?感謝所有反饋。我顯然有點失落。

回答

0

爲什麼不一次解決一個問題?

現在忽略權威人士,並拿出一個ActiveRecord查詢來返回您的期望。

user = User.where(role: :staff).first 
Dispatch.joins(:clinician).merge(User.where(market: user.market)) 

這加入「臨牀醫生」..我相信實際上是「用戶」表幕後因爲模型中的「類:用戶」。 此查詢對於第1部分和第2部分看起來不錯。

運行該AR查詢應首先加入所有與用戶的調度,並只選擇與傳遞給策略的「當前用戶」具有相同「市場」的調度。

但要記住:

@clinicians = policy_scope(Dispatch) 

被派遣回國,而不是「醫生」(用戶),所以在你寫的代碼,「clinicians.id」將是調度表ID是你不什麼」你想要嗎?除非我錯了。

試着讓第一部分和第二部分先工作,如果它還沒有工作。

第3部分「在他們的臨牀醫生檔案中至少有一份保險符合與轉診請求相關的保險。」

聽起來就像你將不得不加入「推薦請求」,並做更多的SQL,我只是不知道你的 域(如has_many:insurance_plans等是如何計算出保險的將需要更多的信息)

+0

感謝您的反饋。當我按照你的建議分配用戶,然後在控制檯中運行第二行時,它會運行但返回空。 user.market返回波士頓,它是id:2。我有在波士頓分貝的臨牀醫生。我可能會誤解,但在我看來,你寫的查詢搜索並返回調度,但我想我想返回的是臨牀醫生名單。我很可能會錯過一些東西,所以請讓我知道你的想法。 – mike9182

+0

我爲保險等添加了其他模型,以便您可以看到它們。 – mike9182