【问题标题】:Is it a temporal relationship bad design?这是一个时间关系不好的设计吗?
【发布时间】:2020-01-10 20:45:11
【问题描述】:

所以目前我有以下 mongoid 模型。

class Delivery
  include Mongoid::Document
  include Mongoid::Timestamps

  field :address, type: String

  has_many :stops
  belongs_to :current_stop, class_name: 'Stop'

  def next
    current_stop = stops.where(completed: false).first
    save
  end
end

class Stop
  include Mongoid::Document
  include Mongoid::Timestamps

  field :address, type: String
  field :completed, type: Boolean, default: false

  belongs_to :delivery, inverse_of: :stops
end

一般的想法是,交付有多个站点,司机需要在其中交付包裹,在交付模型中,我会跟踪正在完成的当前站点。我在 Delivery 模型中使用了 belongs_to 而不是 has_one,因为我想使用使用 has_one 时未生成的生成的访问器 current_stop_id。另一方面,我决定不在 Stop 模型中使用 has_one,因为我不打算使用它,而且当 Stop 完成时,它有两个指向同一个 Delivery 记录的关系可能会令人困惑。

我的问题是,你认为这种 current_stop 时间关系是一个糟糕的设计,因为它会不断变化,并且最终当所有停止完成时将为零?如果是这样,考虑到跟踪正在完成的当前停靠点很重要,您将如何处理这种情况。

【问题讨论】:

    标签: ruby-on-rails database-design mongoid


    【解决方案1】:

    取决于当前停靠点的使用方式,但您的代码已经定义了当前停靠点:

    current_stop = stops.where(completed: false).first
    

    current_stop 方法是获取此信息的另一种方式。如果current_stop 被多个线程更新,这两种方法会导致数据不同步。

    我会说,直到您确实需要当前站点的缓存,查询stops,就像您在需要当前站点时所做的那样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-10
      • 1970-01-01
      • 2011-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-24
      相关资源
      最近更新 更多