Model design question

Hello,

I’m creating an app that will collect and display user-submitted
comments for several different content types. They comment structure
will be the same for each type.

I see two ways of doing this:

  1. define belongs_to relationships with all the different content
    types in my comment.rb model:

belongs_to :type_a
belongs_to :type_b

This would also require a number of foreign keys in my comment table:

type_a_id :integer,
type_b_id :integer,

  1. create a model for each comment application:

CommentTypeA
CommentTypeB

I suppose I am worried about dealing with multiple foreign keys in
approach 1), and I also like the idea of having each comment type
contained in its own class and table, for easier modification down the
road in (second approach). And maybe validation would be easier with
the second approach. But on the other hand, 2) is a lot of repetition
(which of course is so not DRY), and could prove to be risky for other
reasons.

Does anyone with some related experience have any advice?

Thanks very much.

r.g. wrote:

Does anyone with some related experience have any advice?

Polymorphic relationships should work for you…

class Comment
belongs_to :comentable, :polymorphic => true

class TypeA
has_many :comments, :as => :commentable

schema…
commentable_type :string
commentable_id :integer


http://www.5valleys.com/

Thanks a lot! That seems to be working well