Michael Kahle wrote:
> I hope I'm explaining this correctly. Be gentle. Noob here. :)
>
> Ideas on how to solve this? Kind of a chicken or the egg scenario.
> Check out this update function in my controller:
>
> def update
> @snowplow_registration = SnowplowRegistration.find(params[:id])
> @customer = Customer.find(@snowplow_registration.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
>
> 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.
--
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 [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
-~----------~----~----~----~------~----~------~--~---