如何通过多个其他表检索关联记录? 问题

问题描述

这是一个简单的设置,其中用户始终是患者,用户可能也是也可能不是医生:

# user.rb
  has_one :physician
  has_one :patient
# physician.rb
  belongs_to :user
  validates_uniqueness_of :user_id
  has_many :appointments
  has_many :patients,:through => :appointments
# patient.rb
  belongs_to :user
  validates_uniqueness_of :user_id 
  has_many :appointments
  has_many :physicians,:through => :appointments

这一切都连接到约会,然后连接到对话,如下所示:

# appointment.rb
  belongs_to :physician
  belongs_to :patient
  has_one :conversation
  has_many :messages,through: :conversation
# conversation.rb
  belongs_to :appointment
  belongs_to :sender,foreign_key: :sender_id,class_name: "User"
  belongs_to :recipient,foreign_key: :recipient_id,class_name: "User"
  has_many :messages

有时我真的希望能够做到这一点:

current_user.conversations

但这不起作用,相反,我必须做这样的事情:

current_user.physician.appointment.includes(:conversation)
# somehow combine results with this
current_user.patient.appointment.includes(:conversation)

问题

我需要做什么(以及在哪里)以便我可以调用 current_user.conversations 并且它会检索所有对话(即那些作为患者的对话,和 em> 那些作为医生的人(注意用户可能是也可能不是医生)。

注意:如果我的建议不是好的做法,欢迎提出建议。

解决方法

根据您当前的设计,在 User 模型中,您可以简单地为 conversations 添加一个方法:

def conversations
  Conversation.where(sender: self).or(Conversation.where(recipient: self))
end

我不知道为什么对话会有 senderrecipient,因为用户在对话中既可以是(消息的)发送者,又是(消息的)接收者。我会从 sender_id 表中删除 recipient_idconversations,只匹配基于 appointments 的对话。

def conversations
  Conversation
   .joins(appointment: [:physician,:patient])
   .where('physicians.user_id = :user_id or patients.user_id = :user_id',user_id: id)
end