Why is it saved? return true after ActiveRecord :: Rollback?

Example:

BillingProfile.transaction do
  if @billing_profile.save
    unless SomeService.do_something # returns false and rollback occurs
      raise ActiveRecord::Rollback
    end
  end
end

@billing_profile.persisted? # Still return true, despite rollback
@billing_profile.id # Is set, despite rollback

Why does the @billing_profile state not reflect that the record was dropped?

This is a problem because a record cannot be created after it is rolled back.

+4
source share
2 answers

Turns out it was a bug in ActiveRecord (Rails 4): https://github.com/rails/rails/issues/13744

Now it is fixed.

+3
source

I was interested in how transactions work. Your specific scenario is explained in docs . Quoting documents

. . , :

User.transaction do
  User.create(username: 'Kotori')
  User.transaction do
    User.create(username: 'Nemu')
    raise ActiveRecord::Rollback
  end
end

"", "". ActiveRecord:: ROLLBACK. , .

ROLLBACK , - require_new: true. - , - . :

User.transaction do
  User.create(username: 'Kotori')
  User.transaction(requires_new: true) do
    User.create(username: 'Nemu')
    raise ActiveRecord::Rollback
  end
end

"". MySQL PostgreSQL. SQLite3 version >= '3.6.8' .

. , , , true - MS-SQL. - Active Record MySQL PostgreSQL. . Dev.mysql.com/doc/refman/5.6/en/savepoint.html .

0

All Articles