【发布时间】:2013-04-29 00:08:31
【问题描述】:
我无法决定如何使用 rails 关联来建模以下内容。
UML 如下所示:
----------------
| CRITERIA |
----------------
|
|*
----------------
| CONTROLS | <___
---------------- \
^ \
| \
------------------- -------------------
| SCALE CONTROL | | TEXT CONTROL | .....
------------------- -------------------
-各种控件具有完全不同的属性,因此 STI 似乎是一个糟糕的选择。
- 用户可以为每个条件指定任意数量的控件。
我想做如下的事情:
Criteria
has_many :controls
ScaleControl
belongs_to :criteria, as: control
TextControl
belongs_to :criteria, as: control
并且能够查询如下:
criteria.controls
# displays all controls (text, scale, etc.)
criteria.controls.each { ... }
到目前为止我所看到的:
-RailsCasts 关于多态关联的剧集,这似乎不是一个好的用例。
- 数十个 Rails 协会在此处发帖,但未能找到任何直接相关的内容。
-Rails 文档。
在 Rails 中实现上述内容是否有任何通用模式?
【问题讨论】:
标签: ruby-on-rails ruby class inheritance associations