I want to create a very simple database. But I want to know the edit history!
Obviously, we have:
Product
title | price | created_at | updated_at
In my opinion, I add the ‘edition’ column to the table.The edition’s default is 1.
When I use Rails and edit the price column, it will create a new record and the ‘edition’ will increase.
Example:
Product
TITLE | PRICE | CREATED_AT | UPDATED_AT | EDITION
Plane | 12 | 9-19 | NULL | 1
When I edit, a record should be created like this:
Product
TITLE | PRICE | CREATED_AT | UPDATED_AT | EDITION
Plane | 8 | 9-20 | NULL | 2
But I don’t know how to select the data when I want to know the new edition ‘plane’ product.
When I search the edit history can use the code ,like
scope :history, lambda { |tit| where(:title => tit) } //same title
I don’t think this is a best design.
And do you suggest any other thoughts?
The easiest edit map is actually just to store the old data in another table:
Then you simply dump the old values in the Product_History table every time you make an update to the Product table.
The slightly more flexible way is to store the data in an even more denormalized state:
This second variant can be used for recording changes to multiple tables. (Please note, this assumes you don’t have compound primary keys).
In both of these cases you simply query the Product table whenever you want to know the latest value and you query the History table using the primary key of your Product table (combined with the ‘Product’ table name in the
Edit_Historyvariant) when you want to know the history of the changes that this entity has gone through.