Share controller/table

I’m building a type of CMS and have a question about sharing code/
tables. So i have a typical blog setup with a ‘posts’ controller
which has many comments. I need to also make an ‘article’ controller
which is similar to posts but different enough that it needs its own
controller. Comments though will work exactly the same. Rather than
remake the comments table/code (i.e. ‘article_comments’ controller),
i’d prefer to just share the general comments controller/table. Is
there a way to do this?

Table schema for comments table would then be something like:

ID
CommentBody
BelongsToType (i.e. category ‘article’, ‘post’, etc)
ParentID (i.e. a specific ‘articleID’, ‘postID’)

(BelongsToType column wouldnt be needed if I could make the post or
article ID unique across both of those tables though).

Any thoughts?

Thanks so much,
Armen

Armen Arevian wrote:

I’m building a type of CMS and have a question about sharing code/
tables. So i have a typical blog setup with a ‘posts’ controller
which has many comments. I need to also make an ‘article’ controller
which is similar to posts but different enough that it needs its own
controller. Comments though will work exactly the same. Rather than
remake the comments table/code (i.e. ‘article_comments’ controller),
i’d prefer to just share the general comments controller/table. Is
there a way to do this?

Hi Armen

I think you should look into polymorphic associations. Something along
the lines of:

class Comment < ActiveRecord::Base
belongs_to :attachable, :polymorphic => true
end

class Post
has_many :comments, :as => :attachable
end

class Article
has_many :comments, :as => :attachable
end

should work.

Check out the API at