【问题标题】:Active Record Query for subset conditions子集条件的活动记录查询
【发布时间】: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


    【解决方案1】:

    要获得与至少一种保险相匹配的临床医生档案:

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

    然后您可以通过加入临床医生档案来获取用户:

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

    【讨论】:

    • 谢谢 Max - 当我尝试这个时,我在 Dispatches#new 中收到 ActiveRecord 配置错误。具体来说,它说“无法将'用户'加入名为'clinician_profiles'的关联;也许你拼错了?”。当我在控制台中乱来时,我可以先执行 c = User.clinician.first,然后执行 p = c.clinician_profile 之类的操作。 p.user 返回拥有该配置文件的临床医生,但 p.clinician 返回“未定义的方法 'clinician'”。这是问题吗?我需要修复我的模型关联吗?
    • 啊 - 应该是单数的clinician_profile。已编辑。
    • 我还删除了您编辑的双重市场 - 在 @profiles 块中 .group 行中有临床医生的拼写错误,但在更改这些内容后,我收到错误:SQLite3: :SQLException: 没有这样的列:clinicalian_profile.id
    • 它应该是'clinican_profiles.id' - 它是那里的表名,而不是关联,所以它需要是复数。
    • 您的意思是临床医生_profiles.id 吗?与临床医生档案.id
    【解决方案2】:

    以 Rails 的方式进行。 我假设您在保险模型中定义了与 ClinicianProfile 的关联。将 has_many :clinician_profiles, through: insurances 添加到 Patient 并调用 @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
    

    【讨论】:

    • 这没有回答问题。 “我的意图是返回一份与患者共享至少一种保险的临床医生资料列表。”
    • 是的,这正是你的意思。
    • 不 - 他不打算将保险用作连接表。您没有在此处获取域模型。
    • 你认为没有连接表可以吗?
    猜你喜欢
    • 2015-12-12
    • 2013-06-23
    • 2011-08-28
    • 1970-01-01
    • 2012-08-01
    • 1970-01-01
    • 2011-10-17
    • 1970-01-01
    • 2016-03-10
    相关资源
    最近更新 更多