I have several tables, let’s say for example: articles, thoughts and pages and I want to be able to add comments for each one of them.
Should I add comments tables for each on of the table (articles_comments,pages_comments…) or somehow do general comments table for all kinds of data (plz extend how to implement that in rails if you can)
I had an idea to do comments table that contain
[COMMENTS] ID, MODEL, MODEL_ID,
USER_ID, TEXT
while model contain the destination table(articles/posts…) and model_id the foreign id in the table, is it good solution?
the problem is that I have no idea how to implement such model in rails.
I also would like to know how Facebook implemented their posts db.
fb user can post any kind of data (question/status/poll/picture) and it’s just fit in the feeds table with current order and comments.
create a general
Commentmodel and make it polymorphic like this:From an instance of the
Articlemodel, you can retrieve a collection of comments:@article.commentsIf you have an instance of the
Commentmodel, you can get to its parent via@comment.commentableTo make this work, you need to declare both a foreign key column and a type column in the model that declares the polymorphic interface: