On Sep 25, 2:05 pm, Tony <[email protected]> wrote:
> I suspect there is a standard way to handle this issue but I haven't
> found it.
>
> In my controller create action I have a Transaction.  Lets say I'm
> creating a user object here.
>
> def new
>   User.transaction do
>       @user = User.new(params[:user])
>      #do some stuff  set success if no rollback
>   end
>   if success
>    goto success page
>   else
>     render :action => :new
>   end
> end
>
> Now the problem here is when new is rendered @user has an id and it
> treats it as an edit instead of a new.

That means you saved @user to the database, which you shouldn't be
doing in a new action.  The create action is for saving to the
database.

Jeff
purpleworkshops.com

> I could do a the following in the fail block
> @user = User.new(params[:user])
> render :action => :new
>
> but the errors are lost (and any changes or associations i created)
>
> Or I could nil out the id
> @user.id = nil
> render :action => :new
>
> It would be nice if the rollback did this for me (sort of like Object
> Transactions used to but without wiping out the errors and other
> information)?  In my case User has many addresses and many orders so I
> also need to check and nil their IDs.  yuck.
>
> What's the best practice here?
>
> Tony
--~--~---------~--~----~------------~-------~--~----~
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