Matteo C.
2009-Dec-14 18:15 UTC
update a field in a second model when creating a new record in a first model? is possible?
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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
jemminger
2009-Dec-16 04:03 UTC
Re: update a field in a second model when creating a new record in a first model? is possible?
update_attribute performs a save on the model. Instead, just manually set the property: client.date_last_rent = rent.date client.save ? nil : res = false On Dec 14, 1:15 pm, "Matteo C." <mcan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.