Ruby on rails 带关系的单表继承?

Ruby on rails 带关系的单表继承?,ruby-on-rails,ruby-on-rails-3,rails-activerecord,Ruby On Rails,Ruby On Rails 3,Rails Activerecord,我已经找到了一些关于STI的好信息,但还没有看到我想做什么的例子。这是我能想到的最好的例子。我希望能够跟踪两个实体,但它们不需要自己的表。我只需要区分类型(STI可以做到这一点),但我还需要知道一个实体是否以及如何与另一个实体相关。我将以书籍为例。有些书只是书,但其他实体书是多本书的集合 表:书籍 id | title | author_id | book_id 1, 'Fellowship of the Ring, 34, (NULL) # is a physical book 2,

我已经找到了一些关于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中可能吗?如果是,如何最好地实施?我可以在图书模型中说“有很多:图书”吗?他们有什么问题吗


提前谢谢你。

像这样的东西可能适合你的情况

class Book < ActiveRecord::Base
  # all attributes of a book
end

class SeriesBook < Book
  has_many :books, :class_name => "PhysicalBook", :foreign_key => "parent_id"
end

class PhysicalBook < Book
  belongs_to :series, :class_name => "SeriesBook", :foreign_key => "parent_id"
end
你可能会发现你真的想让你的模特与众不同? 系列和书籍,不使用STI?这将使跨这两个应用程序的查询更加复杂,但可能使应用程序的其余部分更容易理解

更新:添加了属于PhysicalBook的,上的类名有很多关联

# How would I query a specific collection to see which and how many books it has within it?
series = SeriesBook.find(0000)
series.books.length # => how many 
series.books.each do |book|
  puts book.title
end

# How would I look at a book and see if it was in a collection and, 
# if so, the specific ID of the specific collection to which it is associated
book = PhysicalBook.find(0000)
puts book.series.title
puts book.series.id 
图书数据库表最终看起来像

id # primary key
title
# other book columns ....
type # STI
parent_id # nullable, used by physical book when part of a series, points to books.id on the series book
另外:请阅读以下内容-

你可能不想要性病?数据模型看起来与上面类似,但没有STI,即系列/书籍


使用外键输入可能会让人困惑:请在此处阅读-

类似的内容可能适合您的情况

class Book < ActiveRecord::Base
  # all attributes of a book
end

class SeriesBook < Book
  has_many :books, :class_name => "PhysicalBook", :foreign_key => "parent_id"
end

class PhysicalBook < Book
  belongs_to :series, :class_name => "SeriesBook", :foreign_key => "parent_id"
end
你可能会发现你真的想让你的模特与众不同? 系列和书籍,不使用STI?这将使跨这两个应用程序的查询更加复杂,但可能使应用程序的其余部分更容易理解

更新:添加了属于PhysicalBook的,上的类名有很多关联

# How would I query a specific collection to see which and how many books it has within it?
series = SeriesBook.find(0000)
series.books.length # => how many 
series.books.each do |book|
  puts book.title
end

# How would I look at a book and see if it was in a collection and, 
# if so, the specific ID of the specific collection to which it is associated
book = PhysicalBook.find(0000)
puts book.series.title
puts book.series.id 
图书数据库表最终看起来像

id # primary key
title
# other book columns ....
type # STI
parent_id # nullable, used by physical book when part of a series, points to books.id on the series book
另外:请阅读以下内容-

你可能不想要性病?数据模型看起来与上面类似,但没有STI,即系列/书籍


外键输入的使用有很多,而且属于什么,可能会让人困惑:在这里读一下-

我想这对我来说不是很清楚。我如何查询一个特定的收藏,看看里面有哪些书和多少本书?我如何看一本书,看它是否在收藏中,如果在收藏中,看它所关联的特定收藏的特定ID?非常有用。谢谢@house9。我想这对我来说不是很清楚。我如何查询一个特定的收藏,看看里面有哪些书和多少本书?我如何看一本书,看它是否在收藏中,如果在收藏中,看它所关联的特定收藏的特定ID?非常有用。谢谢@house9。