【发布时间】:2010-12-11 14:32:43
【问题描述】:
从 Rails 关联指南中,他们使用 has_many 演示了多对多关系:通过如下所示:
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
如何创建和删除约会?
如果我有 @physician,我是否要编写类似以下内容来创建约会?
@patient = @physician.patients.new params[:patient]
@physician.patients << @patient
@patient.save # Is this line needed?
删除或销毁的代码呢?另外,如果一个 Patient 不再存在于 Appointment 表中,它会被销毁吗?
【问题讨论】:
标签: ruby-on-rails activerecord many-to-many has-many-through