Topic: creating and updating records simultaneously

Hi,
I have this following question in rails.
I have 2 tables product and history
product table   
id   name     type                    price     location
1    abc       electronics          $180     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 automatically be reflected in the history status column(as shown above) without the user having to enter that manually.  For example
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 am trying to do something like this though I dont know whether this approach is correct
@product = Product.new(params[:product])
@history= History.new(params[:history])
if @product.save
@history.new(attributes)  ==> I am not sure how to assign the attributes here

I would really appreciate if someone could tell me how a new  history row can be automatically created in this case when a new product is created or an existing product is updated.
Thanks

Re: creating and updating records simultaneously

check out ActiveRecord::Dirty
http://api.rubyonrails.org/classes/Acti … Dirty.html
you can do stuff like

status = ""
if @product.price_changed? then
  status = status + "price changed from #{@product.price_change[0]} to #{@product.price_change[1]}"
end

if @product.location_changed? then
...

you might want to handle this in a before_save filter on the Product model to create the History record?

class Product < ActiveRecord::Base
  before_save :log_history
  ...
 
  private 

  def log_history
     ... # create history object from product changes
  end
end

http://api.rubyonrails.org/classes/Acti … backs.html