简体   繁体   English

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

[英]How to retrieve associated records through more than one other table?

Here's a simple setup where the user will always be a patient, and the user may or may not also be a physician:这是一个简单的设置,用户始终是患者,用户可能是也可能不是医生:

# 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

It all connects to appointments and then to conversations, like so:这一切都连接到约会,然后连接到对话,如下所示:

# 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

Sometimes I really want to be able to do this:有时我真的希望能够做到这一点:

current_user.conversations

but that doesn't work, and instead I would have to do something like this:但这不起作用,相反,我必须做这样的事情:

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

Question问题

What do I need to do (and where) so that I can call current_user.conversations and it will retrieve all the conversations (ie those as a patient, and those as a physician (noting the user may or may not be a physician).我需要做什么(以及在哪里),以便我可以调用current_user.conversations并且它将检索所有对话(即作为患者的对话作为医生的对话(注意用户可能是也可能不是医生)。

Note: open to suggestion if what I'm suggesting isn't good practice.注意:如果我的建议不是好的做法,欢迎提出建议。

Based on your current design, in the User model, you can simply add a method for conversations :根据您当前的设计,在User model 中,您可以简单地添加一个方法进行conversations

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

I am not sure why a conversation would have a sender and recipient as a user can be both a sender (of a message) and recipient (of a message) in a conversation.我不确定为什么对话会有senderrecipient ,因为用户在对话中既可以是(消息的)发件人,也可以是(消息的)收件人。 I would drop the sender_id and recipient_id from the conversations table and just match the conversations based on appointments .我会从conversations表中删除sender_idrecipient_id并根据appointments匹配会话。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以高性能的方式同时检索“关联”记录和“关联通过”记录? - How to retrieve both “associated” records and “associated through” records in a performant way? Rails 3:为什么将具有多个相关记录的重复记录展平? - Rails 3: Why is flatten duplicating records with more than one associated record? 如何使用ActiveRecord中的另一个表的列检索一个表的所有表和另一个表中的所有联接的记录 - How to retrieve all of one table and all joined records in another with the other table's columns in ActiveRecord 是否有比这更有效的方式来加载与列表中的记录关联的记录? - Is there a more efficient way than this to load records associated with records in a list? 属于多个相关记录 - belongs_to more than one associated record 如何连接多个桌子导轨 - How to join more than one table rails 仅检索关联的记录 - Retrieve only associated records 如何使用find_by方法在数据库中检索多个记录 - How to retrieve more than one record in the database with the method find_by 如何计算具有一个或多个关联对象的记录数? - How do I count the number of records that have one or more associated object? Ruby on Rails 3:如何通过关联的方法查找关联的记录? - Ruby on Rails 3: How to find associated records through associated method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM