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.
Pradeep M. (Guest)
on 2007-07-24 19: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
Jacob A. (Guest)
on 2007-07-26 02:46
(Received via mailing list)
Pradeep M. 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 A.
This topic is locked and can not be replied to.