我一直在使用 Rails 中的 STI,并且有一个关于将记录与特定模型相关联的问题。
假设我有一个具有以下架构的产品模型:
# id :integer not null, primary key
# url_name :string(255) not null
# cover_img :string(255)
# created_at :datetime not null
# updated_at :datetime not null
从这个产品模型中,我创建了 2 个继承模型:
福:
class Foo < Product
end
酒吧:
class Bar < Product
end
假设我将一条记录保存到 Foo(A),并将一条记录保存到 Bar(B)。当我调用 Product.all 时,我希望得到所有记录 A 和 B。但是,当我调用 Foo.all 或 Bar.all 时,我希望分别只检索 A 和 B,但是当我调用 Foo.all 时会发生什么或 Bar.all,我得到调用 Product.all 时得到的结果。如何配置这种关系以获得预期的结果?
我正在使用 rails 3.2.6 和 Ruby 1.9.3