【发布时间】:2011-08-18 14:25:54
【问题描述】:
我有
class Car < ActiveRecord::Base; end
class Porsche < Car
has_many :page_items, :as=>:itemable, :dependent=>:destroy
end
我必须提到我正在使用一个名为 cars 的表,其中有一个字段 type。
class PageItem<ActiveRecord::Base
belongs_to :itemable, :polymorphic=>true
end
当我这样做时
a = PageItem.new
a.itemable = Porsche.new
a.itemable
#<Porsche id: nil, type: "Porsche", name: nil, ..etc>
a.itemable_type
=> "Car"
应该是的
a.itemable_type
=> "Porsche"
有人知道吗?
更新
根据bor1s 的回答,这可能是正确的行为。所以如果是这样,那么我的问题是如何将其隐式设置为Porsche?
【问题讨论】:
标签: ruby-on-rails ruby activerecord polymorphic-associations single-table-inheritance