I trying transactions for the first time and trying to figure out the
best (DRY?) way to get the error message outside the transaction block.

If in a controller if I have
def create
  Memo.transaction do
    @memo = Memo.new(params[:memo]) # polymorphic build
    @resource = @memo.memoble # get parent
    @resource.create_memo(@memo) # saves memo and updates parent fields
  end
  ...
  #how do I check if there was a rollback so I can redirect based on
pass fail?
end

In the parents model I have

def create_memo
  #does stuff and if there is an error not in validations
  ...
  raise ActiveRecord::Rollback, 'Memo was NOT created. Payment exceeds
balance due'
  ...
end

I had create_memo return nil or error message and raised the exception
in the controller, but that does not seem to be the best way - but it
works.

The basic question is "How do I tell if a transaction was committed or
rolled back?"

Steve

-- 
Posted via http://www.ruby-forum.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 rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to