# Transaction when operating in two tables

**URL:** <https://www.ruby-forum.com/t/transaction-when-operating-in-two-tables/122634>\
**Category:** Rails I18n\
**Created:** [December 10, 2007, 10:26am UTC](https://www.ruby-forum.com/t/transaction-when-operating-in-two-tables/122634 "2007-12-10T10:26:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 10, 2007, 10:26am UTC](https://www.ruby-forum.com/t/transaction-when-operating-in-two-tables/122634/1 "2007-12-10T10:26:45Z")

</div>

Hello,

I’m reading from a table in the database to fetch some path to files to  
import.

I do a loop for each path, and for each .csv file, I update another  
table.

All works perfect, but it takes lot of time, 90 files and aprox.  
2.000.000 rows.

After adding transactions, the speed has been great, 400% faster.

Now, as I see I have to specify the table for wich the transaction  
occur, where I have to use, the firsst table (where I fetch the path to  
files) or from the other one ?

I update the path file row with a timestamp, rows created/updated/… ,  
so if I problem occurs with some file, I don’t want to update their  
status as done nor having any new data in the other table.

for your info:

table master =\> has the path files  
table dades =\> has de data  
database backend =\> sqlite (only has only one transaction, and not per  
table, per database)

thanks,

raimon

## code:

def import\_file\_nc8\_ajax

```
@txt_to_return = ""
@txt_rows_imported = 0

# delete all the rows in Master for nc8 ...
@result = Arxiu.find_by_sql('DELETE FROM masters')

@txt_to_return += "<p><b>Deleted all rows from NC8 table</b></p>"

@columns = Arxiu.content_columns # id, nc8, nacer

@file_paths=Arxiu.find(:all, :conditions => 'kind="nc8"')

require 'csv'

for arxiu in @file_paths

  @rows_per_file = 0

```

begin

```
Master.transaction do

CSV.open(arxiu.file_path, "r") do |row|

    @data_new=Master.new
    @data_new.nc8 = row[0]
    @data_new.nacer = row[1]
    @data_new.save

    @txt_rows_imported += 1
    @rows_per_file += 1

  end # CSV.open... do

end # transaction

```

rescue

end # begin

```
 arxiu.update_last=Time.now()
 arxiu.update_rows = @rows_per_file
 arxiu.save

```

end

```
  @txt_to_return += "<p><b>Successfully created " +

```

@txt\_rows\_imported.to\_s + " rows

"  
@txt\_to\_return += "

**from " + @file\_paths.size.to\_s + " files**

"

```
render(:text => @txt_to_return)

```

end # import\_file\_nc8\_ajax

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 10, 2007, 10:34am UTC](https://www.ruby-forum.com/t/transaction-when-operating-in-two-tables/122634/2 "2007-12-10T10:34:53Z")

</div>

ah, the question …

the question is, where I have to put the

begin

```
Master.transaction do

```

and if I have to use the Master table or the Dada table

if there are erros, the 99.99% will be in the Dada table, related to the  
imported data from the .csv file …

thanks,

rai

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 10, 2007, 10:36am UTC](https://www.ruby-forum.com/t/transaction-when-operating-in-two-tables/122634/3 "2007-12-10T10:36:35Z")

</div>

Would you please resend this question to rubyonrails-talk? Rails I18n  
is a mailing list about internationalization.

– fxn

---

<div class="post-metadata">

**Author:** ![raimon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/59ef9b/32.png) [@raimon](https://www.ruby-forum.com/u/raimon)\
**Post date:** [December 10, 2007, 10:39am UTC](https://www.ruby-forum.com/t/transaction-when-operating-in-two-tables/122634/4 "2007-12-10T10:39:05Z")

</div>

Xavier N. wrote:

> Would you please resend this question to rubyonrails-talk? Rails I18n  
> is a mailing list about internationalization.

yes, I’m sorry …

rai
