Writing to database row based on column name with FasterCSV.

J

John Mcleod

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
 
J

James Edward Gray II

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 Gray II=
 
J

John Mcleod

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
 
J

James Edward Gray II

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.

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 Gray II
 
J

John Mcleod

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
 
J

James Edward Gray II

Using the .merge method, does this do the actual insert of the new=20
column data? or does it take the data and "merge" it into the csv = before=20
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 Gray II
 
J

John Mcleod

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

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top