【发布时间】:2009-08-31 09:28:00
【问题描述】:
给定两个模型类,Foo 和 Bar,我希望 Foo 使用 3 个不同的属性名称对 Bar 的单独实例进行 3 个引用,外键位于 Foo 表中。 Bar 将单独管理,可以属于 Foo 的多个实例。这在某种程度上解释了它,显然 has_one 是使用错误的关联(我认为?):
Foo
has_one :prop_a, :class_name => "Bar"
has_one :prop_b, :class_name => "Bar"
has_one :prop_c, :class_name => "Bar"
Bar
有 3 种可能的 Bar 类型,由 bar_type 字符串字段表示,对 Foo 的每个引用对应于其中之一。例如Foo.prop_a 引用了一个带有 bar_type = 'type_a' 的 Bar 实例。如何在 Rails 中创建这种类型的关联?
【问题讨论】:
标签: ruby-on-rails activerecord model