Saturday, 23 February 2013

rails sandbox tasks

desc 'do not permanently write on the db (good for testing rake tasks)'
task :sandbox => :environment do
  puts "** << USING SANDBOX!! >> **"

  # beginning
  ActiveRecord::Base.connection.increment_open_transactions
  ActiveRecord::Base.connection.begin_db_transaction

  # end
  at_exit do
     ActiveRecord::Base.connection.rollback_db_transaction
     ActiveRecord::Base.connection.decrement_open_transactions
  end    
end

No comments:

Post a Comment