【问题标题】:Assistance building correct multi-level polymorphic data structure协助建立正确的多级多态数据结构
【发布时间】:2014-05-27 18:07:16
【问题描述】:

我正在为存储应用程序的数据结构而苦苦挣扎。考虑与不使用公共图书馆技术的私人图书馆等效的问题。

  • 一本书在书架上排成一排。
  • 当一本书被借出时,它归读者所有。
  • 当一本书被退回时,我需要知道它应该按行和书架存放在哪里。

我需要跟踪这本书,以便知道它是否被存放、存放在哪里,如果它被借出,谁拥有它,当它被归还时如何处理它。我怎么看是这样的:

Book
has_one :location
has_one :patron, through: :location, source: :library, source_type: 'Patron'
has_one :row, through: :location, source: :library, source_type: 'Row'

Location
belongs_to :book
belongs_to :library, polymorphic: true

Patron
has_many :locations, as: :library
has_many :books, through: :locations

Row
has_many :locations, as: :library
has_many :books, through: :locations

到目前为止,我已经构建了结构,只是我有一些 has_many/belongs_to 关系向后转,这导致我的嵌套表单失败。我正在努力确保这次我做对了。就我而言,这似乎没问题。是吗?

但是,它只涉及行,我需要识别行内的架子。我不知道如何构建该数据结构。我认为货架与位置的关系必须是多态的,但行和货架的关系可以是直接的。那有意义吗? 我不确定它是否正确,因为位置似乎是一个架子,而不是一行中的架子。我正在查看以下内容:

Book
has_one :location
has_one :patron, through: :location, source: :library, source_type: 'Patron'
has_one :shelf, through: :location, source: :library, source_type: 'Shelf'

Location
belongs_to :book
belongs_to :library, polymorphic: true

Patron
has_many :locations, as: :library
has_many :books, through: :locations

Shelf
has_many :locations, as: :library
has_many :books, through: :locations
belongs_to :row

Row
has_many :shelves

感谢您的帮助。

【问题讨论】:

    标签: sql ruby-on-rails polymorphism associations


    【解决方案1】:

    问题中提出的解决方案按设计工作。对外部多态列进行排序存在问题,例如按位置对书籍进行排序,但这似乎只是预期的结果。如果您有兴趣,请参阅24001705 的问题和答案。

    【讨论】:

      猜你喜欢
      • 2017-01-10
      • 2016-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-28
      • 1970-01-01
      • 2019-03-20
      • 2013-08-11
      相关资源
      最近更新 更多