0
我有幾個可評論的模型(文章,文章等)。目前各commentable模型包含以下關聯Rails多態模型 - 基類
has_many :comments, :as => :commentable
和註釋模型包含:
belongs_to :commentable, :polymorphic => true
我commentable模型有一些相似的特點,我想他們是能夠使用一些相同的功能。但是,我認爲MTI(多表繼承)對於這種情況可能是過度的。我可以創建一個他們都繼承的基礎模型類嗎?即:
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
end
class Commentable < ActiveRecord::Base
has_many :comments, :as => :commentable
validates_presence_of :body
def some_function
...
end
end
class Article < Commentable
...
end
class Post < Commentable
...
end
偉大的信息,感謝您的! – 2012-02-26 00:11:36