Jarfis
Jarfis

Reputation: 125

Where do I put rails Transactions and how do I execute them

I have a case where I need to create like 10000 entries in a table and after some research I decided to use a transaction to do it.

My problem is I haven't found any documentation or guide that will tell me where I put a transaction or how I execute it

Upvotes: 0

Views: 237

Answers (1)

Raffael
Raffael

Reputation: 2669

This can be achieved very easily:

ActiveRecord::Base.transaction do 
  ... your code ...
end

The code inside the block will run within a database transaction. If any error occurs during execution, all the changes will be rolled back.

Upvotes: 1

Related Questions