JavierQQ23
JavierQQ23

Reputation: 714

Rails - How to store each update of an item

I'm not sure how to do this in rails (maybe its a common topic but I'm not even sure if the title is correct)

I have a product table with this fields

and the columns that rails provide (id, created_at, updated_at)

This table is going to be updated periodically (lets say each day or so) but I want to save the QUANTITY that is being ADDED, and the date/time of the actualization (UPDATE).

I'm not sure if it's a design problem or something else.

Is there a way rails can handle this?

Thanks in advance

Javier QQ.

Upvotes: 2

Views: 248

Answers (2)

varatis
varatis

Reputation: 14740

Given what you've said in your comments, why don't you just make a new table, say a "Stock" table. Each stock has two fields (in addition to the default created_at and updated_at): quantity and item_id.

Whenever you want to update an item with a new quantity, in the update method (or stock method, whatever it is) you do:

Stock.create(:item_id => @item.id, :quantity => params[:quantity])

This also ensures that you know when stock was added, because Rails will automatically keep track of when this Stock was made.

Upvotes: 2

Veraticus
Veraticus

Reputation: 16064

I'm not sure if this is exactly what you're looking for... but you can try the papertrail Gem. It stores each update of your model and you can easily step backwards or forwards in time through them to inspect your model and what fields changed, so it sounds like it'd be pretty ideal for what you have in mind.

Upvotes: 1

Related Questions