简体   繁体   English

如何在has_many:through关联中访问联接模型的属性

[英]How do I access attributes of the join model in a has_many :through association

Using the example on rails guides : 使用示例在rails guides上

class Physician < ActiveRecord::Base
  has_many :appointments
  has_many :patients, :through => :appointments
end

class Appointment < ActiveRecord::Base
  belongs_to :physician
  belongs_to :patient
end

class Patient < ActiveRecord::Base
  has_many :appointments
  has_many :physicians, :through => :appointments
end

Say Appointment has the attribute exam_room_id . Appointment具有属性exam_room_id I want to use ActiveRecord to obtain a result set that contains a Physician's Patients and what exam room the Patient is in. 我想使用ActiveRecord来获得一个包含Physician's Patients以及Patient所在的检查室的结果集。

I feel like I should be able to do something like this: 我觉得我应该可以做这样的事情:

Physician.first.patients.each do |patient|
  patient.appointment.exam_room_id    
end

This does not work because a Physician has_many :appointments (ie: not a single appointment). 这不起作用,因为Physician has_many :appointments (即:不是一个约会)。

Is there an elegant 'rails way' of accessing the attributes of Appointment together with either side of the has_many :through? 是否有一种优雅的“轨道方法”与has_many :through?任意一侧一起访问Appointment的属性has_many :through?

We can do the following because appointments table is included in the join query implicitly, 我们可以执行以下操作,因为约会表隐式包含在联接查询中,

Physician.first.patients.select("appointments.exam_room_id ")
   .each{|patient| patient.exam_root_id}

You're going to have to step through each appointment. 您将必须完成每个约会。

Physician.first.patients.each do |patient|
  patient.appointments.each {|appointment| puts appointment.exam_room_id}
end

This would show you every exam room a patient has ever been in (based on their appointments). 这将向您显示患者曾经去过的每个检查室(基于他们的约会)。 I don't think is exactly what you're looking for but wanted to illustrate you need to iterate over each physician or patient appointment to get the room_id. 我不认为这正是您要寻找的东西,但想说明您需要遍历每位医生或患者的约会才能获得room_id。

If you're wanting a result set of current rooms you're going to want to go through the appointments. 如果您想要一组当前房间的结果,则需要进行约会。 Physician.first.appointments.each . Physician.first.appointments.each Also, how are you distinguishing between current and past appointments? 另外,您如何区分当前约会和过去约会?

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

相关问题 如何创建has_many:通过关联并设置联接模型属性 - How to create has_many :through association and set join model attributes 如何在has_many中获取模型的属性:通过Rails 5中的关联 - How to get attributes of model in has_many :through association in Rails 5 如何从关联访问联接表属性(has_many:through) - How to access join table attributes (has_many :through) from association 我是否需要has_many的联接表:通过关联? - Do I need a join table for a has_many :through association? 如何在没有其他查询的情况下通过关联对象的相关联接模型访问has_many:? - How can I access a has_many :through association object's relevant join model without additional queries? 如何通过在rails中通过关联对has_many进行关联来实现has_many? - How do I do a has_many through association on a has_many through association in rails? 使用has_many时如何访问rails join模型属性:through - how to access rails join model attributes when using has_many :through 通过以下方式在has_many上优雅地设置联接表属性: - Elegantly set join table attributes on has_many through: association 无法通过关联更新has_many中的联接模型额外属性-Rails - Having trouble in updating join model extra attributes in has_many through association - Rails 如何通过关联在has_many上指定连接表? - How do I specify the join table on a has_many through association?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM