Polymorphic advice for this design

Redhames, got it thanks! Have tested this and now seems to work. Even
the type field is put in automatically when the form submits.

Is this normal that the type field saves as “CitySuggestion” and
“BusinessSuggestion” respectively? Can I change the way it saves, as
“city” or “business”, instead?

Can we talk about the design factor for this. I realise the potential
of STI’s now. However, lets assume this scenario.

Ive got user and product. A user can leave comments, like, upload
products. These are called interactions. On a product page, I need to
display the product and all comments for the product. I also have a
user page with a section that displays all the users recent
interactions, sorted by the created_at date.

I was thinking of using an STI for this, with something like:

class Interaction < ActiveRecord::Base
attr_accessible :user_id, :product_id, :comment, :ip_address, :type

class Comment < Suggestion
attr_accessible :user_id, :product_id, :comment

class Upload < Suggestion
attr_accessible :user_id, :product_id, :ip_address

class Like < Suggestion
attr_accessible :user_id, :product_id

However, I was also considering using a polymorphic behavior. Or in
your opinion, would an STI be more suitable for this?

dont touch the type fields.

If you have a child model that you have to attach to different models
polymorphic associations.
if you have 2 models with a few different fields use STI.

if it guess too clomplex is means you choose wrong and you were so
to use the other kind of association.

thi the case you are saying you dont need any of those, only a comment
that belongs to product and to user like this

class user
has_many comments ## the table should not
a comment_id field
has_many products :through =>comments

class product
has_many comments ## the table should not
a comment_id field
has_many users :through =>comments

class comment
belongs_to :user
belongs_to :product

the coment migration should be like this

create_table :comments do |t|
t.integer :user_id
t.integer :product_id

On Thu, Sep 9, 2010 at 6:09 PM, Christian F. <

watch this


Hi Radhames. Yea, the case you mention above would work. Except, since
you are suggesting I have a comment table on its own. Then I would
need to have a like and upload table on its own as well.

If I needed to list recent interactions (to list down a user feed), I
would have to union join the comment, like and upload tables. If I had
one table called interactions, I could just query this ONE table and
add a where clause (user_id =12, for example) and sort by created_at.
Performance wise, it is better then having to union join 3 other
tables together…

The question is now, whether this should be done using a polymorphic
association or STI

no, with what i showed you you can do

<[email protected] |comment|%>
<%=h comment.content%>
<%comment.products.each do |product|%>


<[email protected] |comment|%>
<%=h comment.content%>
<%comment.users.each do |user|%>

but what do you mean by and upload table???

On Fri, Sep 10, 2010 at 8:39 AM, Christian F. <

i think i dont understand very well because i think that what you are
can be achieve with the code in the previous mail.

this is how i show a user all his comments

<[email protected] |comment|%>
<%=h comment.content%>

and this is how i show a user all the products he has commented on

<[email protected] |comment|%>
<%comment.products.each do |product|%>

would add a like table since liking can only happen once but comments
happen a lot so they are not the same.
this new like table you have the same association as the comments table.

the like table con not be treated as the comment table since a user can
comment a lot on one product , but can only like or dislike once.

On Fri, Sep 10, 2010 at 10:33 PM, Christian F. <

Basically when a user uploads content. Or when a user “likes” a
product or comments on one. These three are interactions. If you are
familiar with Facebook. The homepage displays a sort of user feed.
This is what I am trying to achieve. To have something like this, I
must have a single table. I was thinking of calling it “interaction”.
And then upload, like, and comment all inherit from this table. Sounds