Forum: Ruby on Rails using transaction the right way

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ee0293412faca21e4ebf3326ed9d5f8c?d=identicon&s=25 Kad Kerforn (kadoudal)
on 2009-03-21 12:15
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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-03-21 14:08
(Received via mailing list)
On 21 Mar 2009, at 11:15, Kad Kerforn 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
Ee0293412faca21e4ebf3326ed9d5f8c?d=identicon&s=25 Kad Kerforn (kadoudal)
on 2009-03-21 22:28
Frederick Cheung wrote:
> On 21 Mar 2009, at 11:15, Kad Kerforn 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
This topic is locked and can not be replied to.