私のアプリケーションでは、スタッフのユーザーは患者レコードを作成します。患者モデルは、患者がカバーする保険を記録するための保険モデルとのHABTM関係を有する。アクティブレコードサブセット条件のクエリ
スタッフのユーザーは、患者に属する紹介リクエストを作成します。スタッフのユーザーはまた大学に属し、大学も市場に属します。たとえば、スタッフのユーザーJimはHarvard Universityに属し、Harvard UniversityはMarket Bostonに属します。
臨床医のユーザーは、市場に直接所属しています。
臨床家のユーザーにはそれぞれclinician_profileがあり、そのプロファイルには、保険を記録するための保険モデルとのHABTM関係があります/臨床医が受け入れます。 ClinicianProfileはrole::clinicianというユーザーに属します。
役割が列挙型で定義されています。
enum role: { staff: 0, clinician: 1, admin: 2 }
スタッフのユーザーは、私がリストを表示しようとする新しい派遣/に直接流れ、参照要求を作成するに直接流れ、患者記録を、作成しますその市場にいる臨床医のすべてのうち、患者がカバーしている保険の少なくとも1つを受け入れること。これは、これらの臨床医のそれぞれに紹介依頼と関連する患者の詳細を実際に送信する方法を理解するための道です。私は各ステップで何をしようとしているのか、何がうまくいかないのかを以下にコメントしました。これを行う方法についてのアドバイスは大変ありがたいです。
派遣コントローラー新アクション:
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>
ありがとうマックス - これを試してみると、Dispatches#newでActiveRecord Configurationエラーが出ます。具体的には、「「ユーザー」を「clinician_profiles」という名前のアソシエーションに参加させることはできません。間違っている可能性がありますか?」私はコンソールで混乱すると、私はc = User.clinician.first、次にp = c.clinician_profileのようなことをすることができます。 p.userはプロファイルを所有している臨床家を返しますが、p.clinicianは「未定義のメソッド '臨床家」を返します。これは問題ですか?モデルの関連付けを修正する必要がありますか? – mike9182
Ah - 単数形で 'clinician_profile'にする必要があります。編集されました。 – max
また、編集したダブルマーケットを削除しました。@profilesブロックに.group行の臨床家のスペルミスがありますが、それらの変更後にエラーが発生します:SQLite3 :: SQLException:そのような列はありません: clinician_profile.id – mike9182