Answer the question
In order to leave comments, you need to log in
What is the best way to link data in tables?
There are page
models , blogs , photos .
How to properly arrange them to take with model comments ?
I have two options in mind.
1) In the comments model, add the fields page_id, blog_id, photo_id. In this case, it turns out that in the record there will be only one field with id, the rest will be NULL.
2) In each "parent" model, add the comment_id field. Here I have no idea how to implement the creation / saving.
Which one is better to use and are there any "pitfalls"?
Answer the question
In order to leave comments, you need to log in
Polymorphic link
Gems for comments
Or else - awesome-ruby.com
The pitfalls are that soon you will want to have comments for different entities.
1. It is best to connect a comment service, for example - HyperComments
2. If you still dare to make your bike, then I recommend making 3 tables!
I will explain why:
- comments for pages can contain a quote from the page or some other link. for blogs, this can also be - but for photos there is no such thing!
- comments for photos can have a rating from 0 to 10. and the page and blog may not.
- and there can be many such nuances, you are tormented to fill in 1/3 of the table. and 2/3 will be empty columns.
In short, these are different entities - and it is better to separate them into separate tables in the database. Then there will be no pitfalls!
Quite the first option will suit it (it will use the necessary columns in the right place), but I would probably still share it as suggested by Ivan Filatov
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question