根据这个答案,我正在尝试将 STI 和多态关联一起实现,我的代码:
class Post < ActiveRecord::Base
belongs_to :content, :polymorphic => true
end
class Topic < Post #ActiveRecord::Base
has_one :post, :as => :content, :dependent => :destroy
end
class Tutorial < Post #ActiveRecord::Base
has_one :post, :as => :content, :dependent => :destroy
end
在帖子表中,我有列content_id
,content_type
在表中,主题,教程列body
我如何创建(在 irb 中)新的教程或主题?
我试过了Post.topics.new(..., :content => {body: 'my_text'})
但得到一个错误