我对Rails非常陌生,我只完成了Michael railstutorial.org,现在以教程作为我的新项目的基础,我正在从事简单的电子处方服务。我感到非常困惑的是RoR中的多到多关系,最好避免它们,但我不确定我所需要的功能是否可能做到这一点。
这是我的基本EER与关系表,它将把处方与应该包括的药物联系起来。

我会非常感激任何关于如何简化这个问题的想法,或者它实际上并不难实现?
发布于 2014-04-21 08:15:54
不要回避many-to-many关联,它只是关于使用连接模型
您可以只使用has_many :through

你只需要这样做:
#app/models/prescription.rb
Class Prescription < ActiveRecord::Base
has_many :relations
has_many :medicines, through: :relations
end
#app/models/relation.rb
Class Relation < ActiveRecord::Base
belongs_to :prescription
belongs_to :medicine
end
#app/models/medicine.rb
Class Medicine < ActiveRecord::Base
has_many :relations
has_many :prescriptions, through: :relations
endhttps://stackoverflow.com/questions/23193400
复制相似问题