【发布时间】:2017-10-12 13:36:57
【问题描述】:
在我的应用程序中,员工用户创建患者记录。患者模型与保险模型具有 HABTM 关系,以记录患者所承保的保险。
员工用户创建属于患者的转诊请求。员工用户也属于大学,而大学又属于市场。例如,员工用户 Jim 可能属于哈佛大学,而哈佛大学又属于波士顿市场。
临床医生用户直接属于一个市场。
每个临床医生用户都有一个临床医生档案,他们的档案与保险模型有 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>
【问题讨论】:
标签: ruby-on-rails activerecord