【发布时间】: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