Michael Kahle wrote:

> did-in-fact keep this a transactional operation.  Here is the code I am 
> using now:
> 
>   def update
>     @snowplow_registration = SnowplowRegistration.find(params[:id])
>     @customer = @snowplow_registration.customer
> 
>     @snowplow_registration.attributes = params[:snowplow_registration]
>     @customer.attributes = params[:customer]
> 
> 
>     SnowplowRegistration.transaction do
>       
> @snowplow_registration.update_attributes!(params[:snowplow_registration])
>       @customer.update_attributes!(params[:customer])
>       flash[:notice] = 'Snow Plow Registration was successfully 
> updated.'
>       redirect_to(@snowplow_registration)
>     end
> 
>   rescue ActiveRecord::RecordInvalid => e
>     @customer.valid?
>     render :action => "edit"
>   end

Michael, you no longer have to use update_attributes!, because
this is the same as attributes= followed by save!

>> One little improvement: Use save_without_validation! to prevent
>> the validations from being run twice.
> 
> Another good thought, however what I am using now doesn't have the same 
> duplication as your suggested code does.  Unless of course when I call 
> the "attributes" method it runs a check then as well.  I'm not sure that 
> is the case.  I'll have to think about that.

It's the validation code that's run twice, once at valid? and
once at save!, which you'll see if you put a logger.debug call
in say a Customer.validate method.

This usually doesn't cause problems, only unnecessary extra
CPU load.

-- 
Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.com

--~--~---------~--~----~------------~-------~--~----~
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@googlegroups.com
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