0

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?

4

1 回答 1

1

然后我开始阅读您的问题,我认为首先要分离模型(作为不可破坏或不易破坏的更改),然后在第二步中分离数据库表。

关于评论,我认为您应该有一个与andComment多态关联的模型。AnswerQuestion

因此,在第一步中,您应该将当前Post模型分为:Question、、AnswerComment,但继续使用帖子数据库表(所以我猜这些模型中的 default_scope 设置了正确的 post_type 值)

第二步是(在第一次测试、再次测试甚至可能部署到粉碎测试之后)将每个模型的数据迁移到单独的数据库表中。这将使应用程序设计方式更容易,单个表中的数据更少等。它甚至不应该对性能产生负面影响,因为 sql 查询计数不会改变。

于 2013-07-22T18:49:02.093 回答