Hello all,
I was wondering how to update an existing table column when reading in a
CSV? I’m using FasterCSV and my controller method is this…
-
Import Controller -
def process_csvset file name
file = params[:import][:file]
rowcount = 0Import.transaction do
FasterCSV.parse(file,
:headers => true,
:header_converters => :symbol ) do |row|
Import.create!(row.to_hash)
rowcount += 1
end
endif successful then display, then redirect to index page
flash[:notice] = “Successfully added #{rowcount} project(s).”
redirect_to :action => :indexrescue => exception
file_name = params[:import][‘file’].original_filename
file_parts = params[:import][‘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
endIf an exception in thrown, the transaction rolls back and we end
up in this
# rescue block
flash[:error] = "Error adding projects to Import table. (#{error}).
Please try again. "
redirect_to :action => :new
end
As I read the CSV file, I wish to update a column in the model, mainly
“ProjectType” as the record is being created.
The CSV file does not have this information and there’s no possibility
that it’ll ever have it.
Thank you for any help.
JohnM