Mark Reginald James wrote: > Michael Kahle wrote: >> >> @snowplow_registration.update_attributes!(params[:snowplow_registration]) >> >> When the transaction starts, if validation fails on the >> @snowplow_registration, it doesn't check the validation on my @customer >> for the fields that were updated. Validation for both works properly >> when testing separate... but @customer returns to the edit page without >> passing the changed (params[:customer]) because the failure in the >> transaction happened before I can call >> @customer.update_attributes!(params[:customer]). Is there a way to >> update the model in memory before updating the database? > > This is one way: > > def update > @snowplow_registration = SnowplowRegistration.find(params[:id]) > @customer = @snowplow_registration.customer > > @snowplow_registration.attributes = params[:snowplow_registration] > @customer.attributes = params[:customer] > > customer_valid = @customer.valid? > if @snowplow_registration.valid? && customer_valid > SnowplowRegistration.transaction do > @snowplow_registration.save! > @customer.save! > flash[:notice] = 'Snow Plow Registration was updated.' > redirect_to(@snowplow_registration) > end > else > render :action => :edit > end > end > > If you add "validates_associated :customer" to the > SnowplowRegistration class you only then need to check > the validity of @snowplow_registration, but the validity > of the customer will now be checked whenever a > SnowplowRegistration instance is saved.
Super! Thanks so much. It worked perfectly. The only thing I changed from your example was to handle the update as a transaction. Updating @registration and @customer with the ".attributes" method did the trick perfectly. I am off to experiment with the "validates_associated :customer". Thanks again. Michael -- Posted via http://www.ruby-forum.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 -~----------~----~----~----~------~----~------~--~---