Rails creating and updating 2 model records simultaneously
- by LearnRails
I have 2 tables product and history
product table
id name type price location
1 abc electronics $200 aisle1
history table
id product_id status
1 1 price changed from $200 to $180
Whenever the product price or location is updated by a user by hitting the update button,
1) the changes should be automatically be reflected in the history status column without the user having to enter that manually.
if the price is updated from 200 to 180 then a new history row will be created with new id and the status column will say ' price changed from $200 to $180'
if the location is updated from aisle1 to aisle 2 then status displays ' loc changed from ailse1 to aisle 2'
I tried to
@product = Product.new(params[:product])
@history= History.new(params[:history])
if @product.save
@history.new(attributes) == I am not sure of whether this approach is correct
I would really appreciate if someone could tell me how the history can be automatically updated in this case.