Hi,
i've got a little problem in my app.
When I'm creating a new record in a model, i would like to update a
field in a second model with the value of one of the fields of the
first model.

Example:

# model Rent
...
t.timestamp :date
...

# model Client
...
t.timestamp :date_last_rent
...

When I create a new rent rent, I would like to save the rent.date
value into client.date_last_rent. So in RentController i do this:

 def create
    @rent = Rent.new(params[:rent])

    respond_to do |format|
      if @rent.save and Client.update_date_last_rent(@rent)
          flash[:notice] = "ok"
           ...
      else
         ....
         flash[:error] = "ko"
      end
    end

In my model Client.rb I do this:

  def self.update_fifo_queue(rent)
    res = true

    client = Client.find(line.rent_id)
    client.update_attribute(:date_last_rent => rent.date)
    client.save ? nil : res = false

    return res
  end


It works, but i'm not sure about the code in case of error..... If the
rent.save fail, the client.date_last_value is modified!!

How can i do to avoid this problem? I've read about after_save/
before_save but i never used it and i can't find examples about it!

Any suggestion is appreciated!
Matteo C.

--

You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.


Reply via email to