Vlad
Vlad

Reputation: 8268

Data modeling for comments for posts

I'm working on a publishing tool that has a similar structure to blogs, with a user having many posts and posts having comments, and comments each having its owner, etc.

I don't plan to utilize comments in any aggregate way, which means comments will be just displayed when people view the corresponding post, but won't do much else.

I'm thinking about just storing all the comments as a JSON of {timestamp:{user:comment}} items, so for example, like this:

{{"01-04-12-08" : {"John" : "Hey cool!"} }, {"01-04-12-15":{"Mary" : "No it's not"}}, ... }

Instead of using database tables, it would store the entire data structure as a string, and then later turn it back to JSON when it's needed.

Is it a right way to do something like this? Or should I create database tables? I just felt like creating tables and associations was a bit of an overkill since these comments won't be utilized so much other than just being displayed for each post. Maybe it's not?

Upvotes: 1

Views: 137

Answers (2)

fregas
fregas

Reputation: 3250

If you really prefer working with json and not tables, you might look into a document database like mongodb. However, in my experience, relational databases are a bit more flexible and rails makes it so easy to talk to them.

Upvotes: 2

Mori
Mori

Reputation: 27789

I just felt like creating tables and associations was a bit of an overkill ...

It's really not. It's just easier to keep comments like the rest of your tabular data, rather than reinventing half a wheel in a JSON store. You just have to write a short migration and a minimal model class to get the whole ActiveRecord bag of tricks. And you get an easier path to upgrades, like threads, notifications, spam filtering, voting, etc.

Upvotes: 3

Related Questions