# Writing to database row based on column name with FasterCSV

**URL:** <https://www.ruby-forum.com/t/writing-to-database-row-based-on-column-name-with-fastercsv/182790>\
**Category:** Ruby\
**Created:** [February 2, 2010, 8:22pm UTC](https://www.ruby-forum.com/t/writing-to-database-row-based-on-column-name-with-fastercsv/182790 "2010-02-02T20:22:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 2, 2010, 8:22pm UTC](https://www.ruby-forum.com/t/writing-to-database-row-based-on-column-name-with-fastercsv/182790/1 "2010-02-02T20:22:39Z")

</div>

Hello all,  
I’ve added a couple new columns to a database table. I was trying to  
add default data as the .csv file is being read into the table. I’m not  
sure on how to do this.

- import\_irb\_file -  
def import\_irb\_file

# set file name

file = params[:irb][:file]  
deletecount = 0  
rowcount = 0

# delete all irb records

@irb = Irb.find(:all, :conditions =\> “reconciled = 1”)  
@irb.each { |i|  
i.destroy  
deletecount+=1  
}

Irb.transaction do  
FasterCSV.parse(file,  
:headers =\> true,  
:header\_converters =\> lambda { |h| h.tr(" ",  
“_").delete("^a-zA-Z0-9_”)},  
:converters =\> :all ) do |row|  
Irb.create!(row.to\_hash)  
rowcount += 1  
end  
end  
# if successful then display, then redirect to index page  
flash[:notice] = “Successfully deleted #{deletecount} and added  
#{rowcount} IRB record(s).”  
redirect\_to :action =\> :index

```
rescue => exception
  file_name = params[:irb]['file'].original_filename
  file_parts = params[:irb]['file'].original_filename.split('.')
  ext = file_parts[1]

  if ext != 'csv'
    error = "CSV file is required"
  else
    error = ERB::Util.h(exception.to_s) # get the error and HTML

```

escape it  
end  
# If an exception in thrown, the transaction rolls back and we end  
up in this  
# rescue block

```
  flash[:error] = "Error adding projects to IRB table. (#{error}).

```

Please try again."

```
  redirect_to :controller => 'irbs', :action => 'new'

```

end

Thank you for any and all help with this.

JohnM

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 2, 2010, 8:31pm UTC](https://www.ruby-forum.com/t/writing-to-database-row-based-on-column-name-with-fastercsv/182790/2 "2010-02-02T20:31:58Z")

</div>

On Feb 2, 2010, at 1:22 PM, John M. wrote:

> I’ve added a couple new columns to a database table. I was trying to  
> add default data as the .csv file is being read into the table. I’m not  
> sure on how to do this.

> Thank you for any and all help with this.

I’m happy to try and help, but I’m not sure I understand the question.  
What are you trying to do?

James Edward G. II

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 2, 2010, 8:42pm UTC](https://www.ruby-forum.com/t/writing-to-database-row-based-on-column-name-with-fastercsv/182790/3 "2010-02-02T20:42:28Z")

</div>

Thanks James for the reply.

I need to add default values (0) to 2 columns that are not in the csv.

For example,

```
    Column 1, Column 2, New Column 1, New Column 2
    --------- --------- ------------- ------------

```

Row 1 csv value csv value 0 0

When I go to create a row, I want to check for these column headers and  
write to the table with a zero.

I’m not sure if it’s easier to write to the csv first, adding the  
columns, then parsing to the database or just do it all at once.

John

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 2, 2010, 9:06pm UTC](https://www.ruby-forum.com/t/writing-to-database-row-based-on-column-name-with-fastercsv/182790/4 "2010-02-02T21:06:31Z")

</div>

On Feb 2, 2010, at 1:42 PM, John M. wrote:

> write to the table with a zero.  
> How about changing this code:

Irb.create!(row.to\_hash)

to:

Irb.create!( { “New Column 1” =\> 0,  
“New Column 2” =\> 0 }.merge(row.to\_hash) )

Does that do what you wanted?

James Edward G. II

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 2, 2010, 9:19pm UTC](https://www.ruby-forum.com/t/writing-to-database-row-based-on-column-name-with-fastercsv/182790/5 "2010-02-02T21:19:09Z")

</div>

Yes,  
Thank you James for your expertise.

Just a question to understand.

Using the .merge method, does this do the actual insert of the new  
column data? or does it take the data and “merge” it into the csv before  
the insert?  
Is this in Ruby? I noticed it’s not in the FasterCSV docs.

> Irb.create!( { “New Column 1” =\> 0,  
> “New Column 2” =\> 0 }.merge(row.to\_hash) )

Thanks again.

John

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 2, 2010, 9:27pm UTC](https://www.ruby-forum.com/t/writing-to-database-row-based-on-column-name-with-fastercsv/182790/6 "2010-02-02T21:27:44Z")

</div>

On Feb 2, 2010, at 2:19 PM, John M. wrote:

> Using the .merge method, does this do the actual insert of the new  
> column data? or does it take the data and “merge” it into the csv before  
> the insert?  
> Is this in Ruby? I noticed it’s not in the FasterCSV docs.

merge() is just Hash#merge() from Ruby, yes. It doesn’t change the  
file, no. It just changes the data read before it is handed off to  
ActiveRecord.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 2, 2010, 9:27pm UTC](https://www.ruby-forum.com/t/writing-to-database-row-based-on-column-name-with-fastercsv/182790/7 "2010-02-02T21:27:21Z")

</div>

Well, if I’d do a little detective work, I’d notice that I’m working  
with Hashes. Therefore, the “Hash” class.

John
