Forum: Ruby on Rails transaction

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
407763800e3c974985dc805ef8f2b8cb?d=identicon&s=25 Pradeep Maddi (pradeep)
on 2007-07-24 17:11
Hi all,

I have a scenario where i will uploading data from excel to database. My
condition for uploading is:

If any of the record has an error all the records should be rolled back.

Plz give me suggestions how to implement if we have a loop.

Thanks,
Pradeep
Cb98c59a6ff22c72676e756bbd28f266?d=identicon&s=25 Jacob Atzen (Guest)
on 2007-07-26 00:46
(Received via mailing list)
Pradeep Maddi wrote:
> Hi all,
>
> I have a scenario where i will uploading data from excel to database. My
> condition for uploading is:
>
> If any of the record has an error all the records should be rolled back.
>
> Plz give me suggestions how to implement if we have a loop.

Let's say you have a ExcelData class which is a subclass of
ActiveRecord:

ExcelData.transaction do
   theExcelRecords.each do |currentRecord|
     ExcelData.create!( currentRecord )
   end
end

In this case if any of the currentRecord's are unable to be saved the
whole transaction will be rolled back.

I hope this was the information you were looking for.

--
Cheers,
- Jacob Atzen
This topic is locked and can not be replied to.