【发布时间】:2013-11-13 17:44:08
【问题描述】:
在使用 Rails 3.2.15 的政治分析应用程序中,我有一个 Candidate 模型和一个 District 模型。以下是我尝试建模的关系的细分:
- 候选人属于地区,因为他们在特定地区竞选公职。
- 一名或多名候选人也可以在他们的选区任职。
- 一位现任者是学区的“主要现任者”。
我是这样想的:
class District
has_many :candidates
has_many :candidates, :through => :incumbencies
end
class Candidate
belongs_to: District
end
class Incumbency
belongs_to :district
belongs_to :candidate
# This model has a boolean attribute :is_primary to distinguish primary incumbencies
end
这是正确的方法,还是我会遇到问题?感谢您的帮助。
编辑:
这里有一个例子来说明:
在 1 区
- 候选人 1 是主要在职人员
- 候选人 2 现任
- 候选人 3 是候选人
- 候选人 4 是候选人
在第 2 区
- 候选人 5 是候选人
- 候选人 6 是候选人
在第 3 区
- 候选人 7 是主要在职人员
- 候选人 8 是候选人
【问题讨论】:
标签: ruby-on-rails associations