1
考虑以下几点:在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
如果一个人如此倾斜,可以在:appointments
模型还服务于其他车型呢?例如,我的祖母希望使用一个网站预约她的:dentist
,也可能是:dietitian
(这是一家大医院)。我可以通过所有人的“中间人”:appointments
来做到这一点,或者我必须为每个使用不同的模型?
我自己的应用程序没有使用这些示例,它们只是为了说明。另一个例子是,使用:selection
不仅用于:likes
而且还用于:follow-ees
,例如在Facebook-Twitter联盟中。您选择让两个人都关注,并选择他们喜欢的内容。这可以在Rails中完成吗?
您可能正在寻找多态关联:http://guides.rubyonrails.org/association_basics.html#polymorphic-associations – ChrisBarthol