Making a generic comment model

I have a Comment model obviously to make comments. I have also a
Person and Family class and I want to make comments on bother a person
and a family. How can I set this up. I want to use the same generic
comments table for all the comments on both person and family model. I
can’t figure it out, I can set up comments for either a Person or a
Family one at a time but not both.

Comment
person_id

Class Person
has_many :comments

How can I make comments work on the Family model too?

You want to make comment a polymophic association
e.g. on your person and family model you have:

has_many :comments, :as => :attachable

then on the comment class you have:

belongs_to :attachable, :polymorphic => true

and in your table for comments you have:

integer :attachable_id
string :attachable_type
text :comment

Best, Daniel

thanks you! polymorphic association it’s called you say. It works
now.

On Mon, Mar 8, 2010 at 10:20 PM, eggie5 [email protected] wrote:

thanks you! polymorphic association it’s called you say. It works
now.

You might want to have a look at the acts_as_commentable plugin

It expands on this idea, but uses commentable rather than attachable
as the association name, which I think makes a bit more sense, and is
less likely to collide if you decide that one or more of the models
which can have comments can also have uploaded files as attachments.


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale