Saving a snapshot of the state of several models literally
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. Th开发者_如何学JAVAanks!
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
精彩评论