如何填充字段中的has_many通过连接表 [英] How to populate fields in a has_many through join table
问题描述
我有关于活动记录关联的问题,指的是这部分轨道文档:
的http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association
如果我们有三种型号:
类医师LT;的ActiveRecord :: Base的
的has_many:约会
的has_many:患者:通过=> :约会
结束
类任命<的ActiveRecord :: Base的
belongs_to的:医生
belongs_to的:病人
结束
类患者LT;的ActiveRecord :: Base的
的has_many:约会
的has_many:医生:通过=> :约会
结束
该文件说,加盟模式的集合可以通过API来管理这种方式:
physician.patients =患者
但如果约会模式,就像在链接的例子,有一个叫做appointment_date领域,我想创建给医生和患者在特定的日期一个新的约会? 下面code将在聘任表中创建一个记录,但如何填充appointment_date太在第三步?
医师= Physician.first
病人= Patients.first
physician.patients<<患者
做这样的事情存在?
physician.patients.create(:病人=>耐心,appointment.appointment_time'=> appointment_time)
老问题,但要回答 - 虽然你可以直接分配给 physician.patients
的<<
方法,它创建无值,这可能会或可能不会有效根据业务规则进行预约。因此,更常见的方法来创建该协会将建立,任命了其中的一个
demento = Physician.find_by_name('博士。Demento}
病人= Patient.new {:名称=> '太太。霍洛韦'}
patient.appointments<< Appointment.new {:医生=> demento,:appointment_time => appt_time}
您可以结合线2和3,当然,如果你愿意的话。
该行你指的文档,以
physician.patients =患者
我觉得狭窄的用例可能是,如果Demento有7例,但失去霍洛韦女士由于一个不幸的事件与死亡射线的实验,那么你可以用6现存病人的更新列表做到这一点,他们的任命将是preserved,和夫人Holloway的过去的约会会被自动删除(以便擦除这里的任何纪录,为责任保险的原因?只有Demento会如此卑鄙的)。
I have a question concerning active record association, referring to this part of the rails documentation:
http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association
if we have three models:
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
The documentation says that the collection of join models can be managed via the api this way:
physician.patients = patients
but what if the appointment model, like in the linked example, has a field called appointment_date and I want to create a new appointment given the Physician and the Patient at a specific date? The following code will create a record in the appointment table, but how to populate the appointment_date too in the third step?
physician = Physician.first
patient = Patients.first
physician.patients << patient
does something like this exists?
physician.patients.create( :patient => patient, 'appointment.appointment_time' => appointment_time )
old question, but it should be answered - although you can assign directly to physician.patients
with the <<
method, it creates an appointment with no values, which may or may not be valid depending on the business rules. So the more usual way to create the association would be to build the appointment on one of them
demento = Physician.find_by_name('Dr. Demento'}
patient = Patient.new { :name => 'Mrs. Holloway' }
patient.appointments << Appointment.new { :physician => demento, :appointment_time => appt_time }
you could combine lines 2 and 3 of course if you are so inclined.
the line in the docs you refer to
physician.patients = patients
I think the narrow use case for that might be, if Demento had 7 patients but loses Mrs. Holloway due to an unfortunate incident with a death ray experiment, then you could do this with a updated list of the 6 extant patients and their appointments would be preserved, and Mrs. Holloway's past appointments would be automatically deleted (so as to erase any record of here, for liability insurance reasons? only Demento would be so dastardly).
这篇关于如何填充字段中的has_many通过连接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!