【发布时间】:2012-12-22 18:43:49
【问题描述】:
我找到了一些关于 STI 的好信息,但还没有看到我想做的示例。这是我能想到的最好的例子。我希望能够跟踪两个实体,但它们不需要自己的表。我只需要区分类型(STI 可以做到这一点),但我还需要知道一个实体是否以及如何与另一个实体相关。我将以书籍为例。有些书只是书,但其他实体书是多本书的集合。
桌子:书籍
id | title | author_id | book_id
1, 'Fellowship of the Ring, 34, (NULL) # is a physical book
2, 'The Two Towers', 34, (NULL) # is a physical book
3, 'American Gods', 1, (NULL) # is a physical book
4, 'Complete Lord Of the Rings', 34, (NULL)
5, 'Fellowship of the Ring', 34, 4 # is collected within book id 4
6, 'The Two Towers', 34, 4 # is also collected within book id 4
etc.
所以我希望能够查询所有书籍的书籍,并了解它们如何以及是否通过“book_id”相互关联
这在 Rails 中可行吗?如果是这样,如何最好地实施?我可以在书籍模型中说“has_many :books”吗?他们的问题或顾虑等?
提前谢谢你。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 rails-activerecord