Ruby on Rails回滚除了给定的模型

时间:2018-01-04 14:24:24

标签: ruby-on-rails ruby ruby-on-rails-3 transactions

我在Rails 3.2.22上运行,我无法获得事务/回滚。此代码应始终打印“WORLD”,但实际上,如果调用 raise ,它将永远不会起作用。但根据Rails doc,它应该始终到达那里。我的问题是 - 有没有办法将@pb状态保存到数据库中?如果我决定在回滚之前保存状态,则不会保存。如果我在加注后保存它,它将不会执行。有办法吗?例如,是否可以允许函数“在块中跳出块”?

    @pb = ProgressBar.create(..)
    ..
    def import(user)
    Thread.new do
      ActiveRecord::Base.transaction do
        errs = operation(user)
        Rails.logger.info "HELLO"
        if errs.any?
          raise ActiveRecord::Rollback
        end
      end
      Rails.logger.info "WORLD"
      @pb.failed_import
    end
  end

0 个答案:

没有答案