Ruby on rails 这是一个不好的设计吗?
因此,目前我有以下mongoid模型Ruby on rails 这是一个不好的设计吗?,ruby-on-rails,database-design,mongoid,Ruby On Rails,Database Design,Mongoid,因此,目前我有以下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)
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
一般的想法是,一个交付有多个站点,司机需要交付包裹,在交付模型中,我跟踪当前完成的站点。在传递模型中,我使用了“属于”而不是“拥有”,因为我想使用生成的访问器当前“停止”id,该id在使用“拥有”时未生成。另一方面,我决定在止损模型中不使用has_one,因为我不打算使用它,而且当止损完成时,它有两个指向同一交付记录的关系,这可能会让人困惑
我的问题是,你是否认为当前的_-stop时间关系是一个糟糕的设计,考虑到它将不断变化,并且在所有停止结束时将是零?如果是,考虑到跟踪正在完成的当前停止非常重要,您将如何处理这种情况。取决于当前停止的使用方式,但您的代码已经有了当前停止的定义:
current_stop = stops.where(completed: false).first
current\u stop
方法是获取此信息的另一种方法。这两种方法可以使数据不同步,比如说,如果多个线程更新了当前停止
我会说,在您确实需要当前站点的缓存之前,请像您在需要当前站点时所做的那样查询站点