Question

I'm currently refactoring my rails app. The tricky part so far is the table posts.

In the current version I'm using posts for:

  • questions
  • answers
  • comments

Using the post_type attribute.

Relationships:

  • Questions have many answers and comments.
  • Answers have many comments.
  • Answer belongs to a question.
  • Comment belongs to either an answer or a question.

So far I was splitting the question and answer post types into seperate models, using the same table: posts. But with comments I have the following problem:

Every Post, but comments, is commentable. Would it be a good idea to create an additional comments table and create a polymorphic association 'commentable' to each of the post types instead of inheriting the posts table?

Was it helpful?

Solution

Then I started reading you question the first thing I thought was to first separate models (as a non- or less-destructible change) and then in a second step to separate db tables.

Concerning comments I think you should have a Comment model that is polymorphically associated with Answer and Question.

So in the first step you should separate current Post model into: Question, Answer, and Comment, but keep using posts db table (so I guess default_scope in each of these models setting proper post_type value)

Second step would be (after first is tested, tested again and probably even deployed to crush test) to migrate data for each model into separate db table. That will make app design way easier, less data in a single table, etc. It shouldn't even have negative performance impact as sql query count will not change.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top