# Using transaction the right way

**URL:** <https://www.ruby-forum.com/t/using-transaction-the-right-way/163522>\
**Category:** Rails\
**Created:** [March 21, 2009, 12:15pm UTC](https://www.ruby-forum.com/t/using-transaction-the-right-way/163522 "2009-03-21T12:15:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [March 21, 2009, 12:15pm UTC](https://www.ruby-forum.com/t/using-transaction-the-right-way/163522/1 "2009-03-21T12:15:22Z")

</div>

I wonder if my transaction is well written ?  
I create the user record first , then the issue record (no validation)  
if the user record is valid and saved  
if anything happen when saving the issue, I want to rollback the user  
record…

def infos\_send  
@franchise = Franchise.scoped\_by\_mother\_of\_all(true).first  
@user = ProspectUser.new(params[:user])

```
User.transaction do
  if @user.save
    create_info_request_issue #private method
    @issue.save
    raise ActiveRecord::Rollback
    flash[:notice] = I18n.t(:notice_successful)
    redirect_back_or_default :action => 'index'
  else
    render :action => 'infos'
  end
end

```

end

thanks for you help

erwin

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [March 21, 2009, 2:08pm UTC](https://www.ruby-forum.com/t/using-transaction-the-right-way/163522/2 "2009-03-21T14:08:10Z")

</div>

On 21 Mar 2009, at 11:15, Kad K. wrote:

> I wonder if my transaction is well written ?  
> I create the user record first , then the issue record (no validation)  
> if the user record is valid and saved  
> if anything happen when saving the issue, I want to rollback the user  
> record…

It looks a bit odd - you always raise ActiveRecord::Rollback, no  
matter what happened.  
In case you did’t know, if an exception is raised inside the  
tranasaction block then a rollback will happen for you automatically.

Fred

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [March 21, 2009, 10:28pm UTC](https://www.ruby-forum.com/t/using-transaction-the-right-way/163522/3 "2009-03-21T22:28:41Z")

</div>

Frederick C. wrote:

> On 21 Mar 2009, at 11:15, Kad K. wrote:
> 
> > I wonder if my transaction is well written ?  
> > I create the user record first , then the issue record (no validation)  
> > if the user record is valid and saved  
> > if anything happen when saving the issue, I want to rollback the user  
> > record…
> 
> It looks a bit odd - you always raise ActiveRecord::Rollback, no  
> matter what happened.  
> In case you did’t know, if an exception is raised inside the  
> tranasaction block then a rollback will happen for you automatically.
> 
> Fred

Yes sorry… this line should have been deleted…  
is the remaining code OK ?

thanks
