从字面上保存多个模型状态的快照

发布于 2024-10-19 17:50:00 字数 224 浏览 1 评论 0原文

我对编程相当陌生,我正在 Rails 中为分析实验室开发一个簿记应用程序。记录维护事件时,从字面上保存关联模型中某些数据的快照会很有用。换句话说,我们有仪器、仪器的测试方法以及在仪器上执行的维护事件,但方法是可变的,因此在执行维护时最好有一个方法状态的静态记录。

简单地创建一个单独的快照/状态模型来仅存储来自关联模型的数据,这是好的数据库设计吗?这对我来说是有道理的,但我对编程还很陌生,所以要警惕养成坏习惯。谢谢!

I'm fairly new to programming, and I'm developing a bookkeeping app in Rails for an analytical lab. When logging maintenance events, it would be useful to save a snapshot of certain data from associated models literally. In other words we have instruments, testing methodologies for the instruments, and maintenance events performed on the instruments, but methodologies are changeable, so it would be nice to have a static record of the state of the methodology when a maintenance is performed.

Is it good database design to simply create a separate snapshot/state model that just stores the data from the associated models literally? This makes sense to me but I'm new enough to programming to be wary of forming bad habits. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清君侧 2024-10-26 17:50:00

我想您有模型 InstrumentMethodologyMaintenanceEvent。我将简单地为每个维护事件分配已使用的方法:

class Instrument < ActiveRecord::Base
  has_many :maintenance_events
end

class MaintenanceEvent < ActiveRecord::Base
  belongs_to :instrument
  has_one :methodology
end

class Methodology < ActiveRecord::Base
  belongs_to :maintenance_event
end

然后您将能够访问在特定仪器上的维护事件期间使用的方法。例如,要了解显微镜在上次维护期间使用了什么方法:

Instrument.find_by_name("Microscope XY").maintenance_events.last.methodology

I suppose you have the models Instrument, Methodology and MaintenanceEvent. I would simply assign to each maintenance event the methodology that has been used:

class Instrument < ActiveRecord::Base
  has_many :maintenance_events
end

class MaintenanceEvent < ActiveRecord::Base
  belongs_to :instrument
  has_one :methodology
end

class Methodology < ActiveRecord::Base
  belongs_to :maintenance_event
end

Then you'll be able to access the methodology used during a maintenance event on a certain instrument. For example, to know what methodology has been used on a microscope during its last maintenance:

Instrument.find_by_name("Microscope XY").maintenance_events.last.methodology
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文