Daniel Huckstep
Daniel Huckstep

Reputation: 5408

Disable transactions in ActiveRecord

How do I disable transactions in Rails' ActiveRecord? I have a specific situation where I want them to go away I can't seem to find anything useful out there. Is it even possible?

Upvotes: 3

Views: 4740

Answers (3)

Owen Kim
Owen Kim

Reputation: 11

Similar to Daniel's answer but I found I had to also disable savepoints to function. Tested on Rails 3.2.22.2.

ActiveRecord::ConnectionAdapters::Mysql2Adapter.class_eval do
  def begin_db_transaction
  end

  def commit_db_transaction
  end

  def create_savepoint
  end

  def rollback_to_savepoint
  end

  def release_savepoint
  end
end

Upvotes: 1

Daniel Huckstep
Daniel Huckstep

Reputation: 5408

Poor man's "no transactions"

# Force the loading of AR stuff
ActiveRecord::Base.connection.execute('SELECT 1')

# Remove transactions
ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do
  def begin_db_transaction
  end

  def commit_db_transaction
  end
end

Upvotes: 3

mcostanza
mcostanza

Reputation: 15

transaction is a class method on ActiveRecord::Base so you can do it like this:

Model.transaction do
  ...
end

or if you prefer to do it without a specific model:

ActiveRecord::Base.transaction do
  ...
end

It may also depend on which database you are using, I know for sure that works on mysql but not sure about others.

Upvotes: -3

Related Questions