# Object has invalid ID after rollback

**URL:** <https://www.ruby-forum.com/t/object-has-invalid-id-after-rollback/176177>\
**Category:** Rails\
**Created:** [September 25, 2009, 9:07pm UTC](https://www.ruby-forum.com/t/object-has-invalid-id-after-rollback/176177 "2009-09-25T21:07:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tnyplz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@tnyplz](https://www.ruby-forum.com/u/tnyplz)\
**Post date:** [September 25, 2009, 9:07pm UTC](https://www.ruby-forum.com/t/object-has-invalid-id-after-rollback/176177/1 "2009-09-25T21:07:31Z")

</div>

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.

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

---

<div class="post-metadata">

**Author:** ![tnyplz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@tnyplz](https://www.ruby-forum.com/u/tnyplz)\
**Post date:** [September 25, 2009, 9:26pm UTC](https://www.ruby-forum.com/t/object-has-invalid-id-after-rollback/176177/2 "2009-09-25T21:26:15Z")

</div>

actually making the id nil doesn’t work either. guess it still has  
created\_at and updated\_at items. Anyway. Is there a simple way to  
remedy this issue?

---

<div class="post-metadata">

**Author:** ![tnyplz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@tnyplz](https://www.ruby-forum.com/u/tnyplz)\
**Post date:** [September 25, 2009, 9:53pm UTC](https://www.ruby-forum.com/t/object-has-invalid-id-after-rollback/176177/3 "2009-09-25T21:53:45Z")

</div>

On Sep 25, 2:05 pm, Tony [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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](http://purpleworkshops.com)

---

<div class="post-metadata">

**Author:** ![tnyplz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@tnyplz](https://www.ruby-forum.com/u/tnyplz)\
**Post date:** [September 25, 2009, 10:32pm UTC](https://www.ruby-forum.com/t/object-has-invalid-id-after-rollback/176177/4 "2009-09-25T22:32:58Z")

</div>

On Sep 25, 3:44 pm, Jeff [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > def new
> 
> > 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.

I didn’t save on the new action. The user did a create and after  
writing to the database there was an issue so I aborted the  
transaction and sent the user back to the new form (this is the  
standard practice when there is an error on create).

My issue is that my objects now have bogus IDs in them causing the  
view to treat the object as created.

To get around this I now recreate all my objects from the params and  
run valid? before rendering the new form so any errors will appear (I  
guess the save would have failed if there were errors on the objects  
themselves so calling valid is probably not needed).

---

<div class="post-metadata">

**Author:** ![tnyplz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@tnyplz](https://www.ruby-forum.com/u/tnyplz)\
**Post date:** [September 29, 2009, 2:54pm UTC](https://www.ruby-forum.com/t/object-has-invalid-id-after-rollback/176177/5 "2009-09-29T14:54:39Z")

</div>

FYI. It appears there is a ticket on this issue

[https://rails.lighthouseapp.com/projects/8994/tickets/2991-after-transaction-patch](https://rails.lighthouseapp.com/projects/8994/tickets/2991-after-transaction-patch)
