STI and polymorphs

前端 未结 5 1377
孤独总比滥情好
孤独总比滥情好 2021-01-04 13:53

I have problem with my code

class Post < ActiveRecord::Base
end

class NewsArticle < Post
  has_many :comments, :as => :commentable, :dependent =>         


        
5条回答
  •  时光取名叫无心
    2021-01-04 14:20

    Good question. I had exactly the same problem using Rails 3.1. Looks the problem is not solved yet. Apparently, using polymorphic associations in combination with Single Table Inheritance (STI) in Rails is a bit complicated.

    The current Rails documentation for Rails 3.2 gives this advice for combining polymorphic associations and STI:

    Using polymorphic associations in combination with single table inheritance (STI) is a little tricky. In order for the associations to work as expected, ensure that you store the base model for the STI models in the type column of the polymorphic association.

    In your case the base model would be "Post", i.e. "commentable_type" should be "Post" for all comments.

提交回复
热议问题