Forum: Ruby on Rails How to restart AR transaction?

7a9b386543fc291c2c4942c4c63b4559?d=identicon&s=25 Ken Ip (Guest)
on 2013-06-03 17:29
(Received via mailing list)
http://edgeapi.rubyonrails.org/classes/ActiveRecor...
under
"Exception handling and rolling back" says that "*One should restart the
entire transaction if an ActiveRecord::StatementInvalid occurred.*" My
question is how? I tried wrapping the statements in two separate
transaction block but that still doesn't work.

I have something like:
    self.transaction do
      bar = create!(params) rescue nil
    end
    unless bar
      self.transaction do
        bar = find_or_initialize_by(foo: foo)
        bar.update(params)
      end

    end

and PG would still complain about it with "PG::Error: ERROR: current
transaction is aborted, commands ignored until end of transaction
block".
So my question is whether there is a proper way of restarting the
transaction? Thanks!

Ken
15eb13d662803f57bc4aea59704988b4?d=identicon&s=25 Scott Ribe (Guest)
on 2013-06-03 17:58
(Received via mailing list)
On Jun 3, 2013, at 9:27 AM, Ken Ip wrote:

> and PG would still complain about it with "PG::Error: ERROR: current transaction
is aborted, commands ignored until end of transaction block". So my question is
whether there is a proper way of restarting the transaction? Thanks!

rollback

--
Scott Ribe
scott_ribe@elevated-dev.com
http://www.elevated-dev.com/
(303) 722-0567 voice
7a9b386543fc291c2c4942c4c63b4559?d=identicon&s=25 Ken Ip (Guest)
on 2013-06-04 05:03
(Received via mailing list)
Thanks for the reply, Scott. Can you show me how? I've tried per
this<http://api.rubyonrails.org/classes/ActiveRecord/Ro... but
it still doesn't work.


self.transaction do
  bar = create(params) rescue nil
  raise ActiveRecord::Rollback unless bar
end
unless bar
  self.transaction do
    bar = find_or_initialize_by(foo: foo)
    bar.update(params)
  end

end
15eb13d662803f57bc4aea59704988b4?d=identicon&s=25 Scott Ribe (Guest)
on 2013-06-25 17:43
(Received via mailing list)
Look at the actual SQL in the logs.

On Jun 3, 2013, at 9:01 PM, Ken Ip wrote:

>     bar.update(params)
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email
to rubyonrails-talk+unsubscribe@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk....
> For more options, visit https://groups.google.com/groups/opt_out.
>
>


--
Scott Ribe
scott_ribe@elevated-dev.com
http://www.elevated-dev.com/
(303) 722-0567 voice
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.