ruby-on-rails - 如何通过多个其他表检索关联记录?
问题描述
这是一个简单的设置,用户始终是患者,用户可能是也可能不是医生:
# 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
,它会检索所有对话(即作为患者的对话和作为医生的对话(注意用户可能是也可能不是医生)。
注意:如果我的建议不是好的做法,欢迎提出建议。
解决方案
根据您当前的设计,在User
模型中,您可以简单地添加一个方法conversations
:
def conversations
Conversation.where(sender: self).or(Conversation.where(recipient: self))
end
我不确定为什么对话会有一个sender
,并且recipient
作为用户,在对话中既可以是(消息的)发送者,也可以是(消息的)接收者。我会从表中删除sender_id
and并仅匹配基于 的对话。recipient_id
conversations
appointments
def conversations
Conversation
.joins(appointment: [:physician, :patient])
.where('physicians.user_id = :user_id or patients.user_id = :user_id', user_id: id)
end
推荐阅读
- session - NextJS 和 NextAuth 会话用户对象由于 [...nextauth.ts] 被触发重新编译而丢失
- java - TargetDataline 返回 -1(Audiosystem.NOT_SPECIFIED)
- python - 将无人机 (UAV) 入侵检测数据集下载并导入 Jupiter Notebook
- node.js - Express 多路由匹配引发错误 [ERR_HTTP_HEADERS_SENT]:在将标头发送到客户端后无法设置标头
- javascript - 减少():类型“数字”不可分配给类型“项目”
- r - 无论数据框如何,都在 ggplot 上放置一个点
- testing - 使用 Testcafe 时无法在调试模式下编辑付款 iframe
- google-apps-script - 在其他 Google 表格标签中搜索数据并报告它出现在哪个标签中?
- c# - if 语句模式
- javascript - 如何使用 Express 在 javascript 中进行一般补丁?