【发布时间】:2011-10-23 10:58:13
【问题描述】:
使用the Rails guides 中的这个修改示例,如何使用 mongoid 对关系“has_many :through”关联进行建模?
挑战在于 mongoid 不像 ActiveRecord 那样支持 has_many :through。
# doctor checking out patient
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, :through => :appointments
has_many :meeting_notes, :through => :appointments
end
# notes taken during the appointment
class MeetingNote < ActiveRecord::Base
has_many :appointments
has_many :patients, :through => :appointments
has_many :physicians, :through => :appointments
end
# the patient
class Patient < ActiveRecord::Base
has_many :appointments
has_many :physicians, :through => :appointments
has_many :meeting_notes, :through => :appointments
end
# the appointment
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
belongs_to :meeting_note
# has timestamp attribute
end
【问题讨论】:
标签: ruby-on-rails activerecord mongodb data-modeling mongoid