在我的應用程序中,員工用戶創建患者記錄。患者模型與保險模型具有HABTM關係,以記錄患者所覆蓋的保險。針對子集條件的活動記錄查詢
員工用戶創建屬於患者的推薦請求。職員用戶也屬於大學,而大學又屬於市場。例如,職員用戶吉姆可能屬於哈佛大學,而哈佛大學又屬於波士頓市場。
臨牀醫生用戶直接屬於市場。
臨牀醫生用戶每個人都有一個臨牀醫師簡介,他們的檔案與保險模型有HABTM關係,以記錄保險/臨牀醫生接受的保險。 ClinicianProfile屬於用戶角色::臨牀醫師。
的角色與一個枚舉定義:
enum role: { staff: 0, clinician: 1, admin: 2 }
甲人員用戶創建患者記錄,它直接流入創建推薦請求,它直接流入分派/新我打算顯示一個列表的所有臨牀醫生在其市場中接受至少一種病人所覆蓋的保險。這正在找出如何實際向這些臨牀醫生髮送轉診請求和相關患者詳細信息的途徑。我在下面評論了我正在嘗試對每一步做什麼,以及哪些行不通 - 對於如何做到這一點的任何建議將不勝感激。
急件控制器的新行動:
def new
#This works
@patient = @referral_request.patient
#This works
@user = current_user
#This works - returns a list of clinician users who match the current user's university's market.
@market_matched_clinicians = User.clinician.where(market: @user.university.market)
#This doesn't work -My intention is for this to return a list of clinician profiles that share at least one insurance with the patient.
@matched_clinician_profiles = ClinicianProfile.where(insurances: [@patient.insurances])
#Once I have all of the matched clinician profiles, how can I use that to return the corresponding clinician users?
# @matched_clinicians = ???
@dispatch = @referral_request.dispatches.new
end
型號:
class User < ApplicationRecord
include Clearance::User
include StaffUser
include ClinicianUser
include AdminUser
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 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
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'
validates :staff_doctor_id, presence: true
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
這裏就是我試圖使這些結果的看法:
<% provide(:title, "New Dispatch") %>
<h2> These clinicians match your market and your patient's insurance </h2>
<table class="table table-striped">
<thead>
<tr>
<th>Provider ID</th>
<th>Name</th>
<th>Provider Market</th>
<th>Insurances Accepted</th>
<th>Gender/s</th>
<th>Race/Ethnicity</th>
<th>Languages</th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<td><%= user.id %></td>
<td><%= user.name %></td>
<td><%= user.market.name %></td>
<td><%= user.clinician_profile.insurances.map(&:name).to_sentence %></td>
<td><%= user.clinician_profile.genders.map(&:name).to_sentence %></td>
<td><%= user.clinician_profile.races.map(&:name).to_sentence %></td>
<td><%= user.clinician_profile.languages.map(&:name).to_sentence %></td>
</tr>
<% end %>
</tbody>
</table>
感謝馬克斯 - 當我嘗試這個我得到一個ActiveRecord配置錯誤在發送#新。具體來說,它說:「不能加入'用戶'關聯名爲'clinician_profiles';也許你拼錯了它?」。當我在控制檯中亂搞時,我可以做一些事情,比如c = User.clinician.first,然後是p = c.clinician_profile。 p.user返回擁有該配置文件的臨牀醫生,但p.clinician返回「未定義方法的臨牀醫生」。這是問題嗎?我需要修復我的模型關聯嗎? – mike9182
啊 - 應該是'clinician_profile'單數。編輯。 – max
我也刪除了你編輯出來的雙重市場 - 在@profiles塊有一個臨牀醫生拼寫錯誤,但在改變這些事情後,我得到的錯誤:SQLite3 :: SQLException:沒有這樣的列: clinician_profile.id – mike9182