dont touch the type fields.
If you have a child model that you have to attach to different models use 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 suppose to use the other kind of association.
thi the case you are saying you dont need any of those, only a comment model that belongs to product and to user like this
class user
has_many comments ## the table should not have a comment_id field
has_many products :through =>comments
class product
has_many comments ## the table should not have 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
t.content
t.timestamp
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
<%@user.comments.each |comment|%>
<%=h comment.content%>
<%comment.products.each do |product|%>
<%=product.name%>
<%end%>
<%end%>
also
<%@product.comments.each |comment|%>
<%=h comment.content%>
<%comment.users.each do |user|%>
<%=user.name%>%>
<%end%>
but what do you mean by and upload table???
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
good?
i think i dont understand very well because i think that what you are saying can be achieve with the code in the previous mail.
this is how i show a user all his comments
<%@user.comments.each |comment|%>
<%=h comment.content%>
<%end%>
and this is how i show a user all the products he has commented on
<%@user.comments.each |comment|%>
<%comment.products.each do |product|%>
<%=product.name%>
<%end%>
<%end%>
would add a like table since liking can only happen once but comments can 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.