子条件的活动记录查询

时间:2017-10-12 13:36:58

标签: ruby-on-rails activerecord

在我的应用程序中,员工用户创建患者记录。患者模型与保险模型具有HABTM关系,以记录患者所享有的保险。

员工用户创建属于患者的推荐请求。员工用户也属于大学,而大学又属于市场。例如,工作人员吉姆可能属于哈佛大学,而哈佛大学则属于波士顿市场。

临床医生用户直接属于市场。

临床医生用户每人都有一个clinician_profile,他们的个人资料与保险模型有HABTM关系,以记录临床医生接受的保险。 ClinicianProfile属于用户,其中role :: clinician。

角色定义为枚举:

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

员工用户创建患者记录,该记录直接流入创建推荐请求,该请求直接流入dispatches / new,我打算显示其市场中所有临床医生的列表并接受至少一个患者所享有的保险。这是在确定如何实际向每位临床医生发送推荐请求和相关患者详细信息的途径。我已经在下面评论了我正在尝试做的每一步以及什么是无效的 - 任何关于如何做到这一点的建议将不胜感激。

调度控制器新操作:

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>

2 个答案:

答案 0 :(得分:1)

获取至少与一项保险相匹配的ClinicianProfiles:

@profiles = ClinicianProfile.joins(:insurances)
                            .where(insurances: { id: @patient.insurances })
                            .group('clinician_profiles.id')
                            .having("count(*) = 1")

然后,您可以通过加入clinician_profiles来获取用户:

@users = User.joins(:clinician_profile)
             .where(
               market: @user.university.market,
               clinician_profile: { id: @profiles }
             )

答案 1 :(得分:0)

以Rails方式执行。 我假设您在保险模型中定义了与ClinicianProfile的关联。将has_many :clinician_profiles, through: insurances添加到患者并致电@ patient.clinician_profiles。

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 :clinician_profiles, through: insurances
  has_many :referral_requests
  belongs_to :staff_doctor, class_name: 'User', foreign_key: 'staff_doctor_id'

  validates :staff_doctor_id, presence: true