From: James Edward Gray II on
On Nov 17, 2009, at 10:38 AM, John Mcleod wrote:

> James,
> Thank you for replying.
>
> sample headers:
> Q.20C - Population May Include (Target)
> IRB#
>
> Desired headers:
> Q20C_Population_May_Include_Target
> IRB_id

Try adding this argument where faster CSV opens the file:

:header_converters => lambda { |h| h.tr(" ", "_").delete("^a-zA-Z0-9_") }

Hope that helps.

James Edward Gray II

From: Marnen Laibow-Koser on
[Please do not top-post.]

John Mcleod wrote:
> Hello Marnen,
> Thank you for replying.
>
> I would consider the non-use of special characters in a database column
> title, a matter of good form.
> The only special character I use in database column titles is an
> underscore and sometimes not that.

I agree with you when I'm creating the column names manually. But if
you're loading them dynamically from a CSV file, then it probably makes
sense to transform the names as little as possible

>
> John
>


Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen(a)marnen.org
--
Posted via http://www.ruby-forum.com/.

From: John Mcleod on
I'm not sure if the placement is correct but I'm still getting "Error
adding projects to IRB table. (unknown attribute:
Q16_Research_Category_International). Please try again." errors.

Here's my updated code.

def import_irb_file
# set file name
file = params[:irb][:file]
rowcount = 0

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 added #{rowcount} project(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

John

--
Posted via http://www.ruby-forum.com/.

From: James Edward Gray II on
On Nov 17, 2009, at 12:26 PM, John Mcleod wrote:

> I'm not sure if the placement is correct

You're placement looks fine to me.

> but I'm still getting "Error
> adding projects to IRB table. (unknown attribute:
> Q16_Research_Category_International). Please try again." errors.

The name Q16_Research_Category_International doesn't have any characters in it. It looks like what you asked me for, but the table doesn't seem to have that column. So, you tell me what we did wrong. :)

James Edward Gray II
From: John Mcleod on
You are correct.
I need to check all my DB column titles. I updated the column title and
the next error was another DB column.

In your code...
:header_converters => lambda { |h| h.tr(" ",
"_").delete("^a-zA-Z0-9_")},

what is happening here?
h.tr(" ", "_").delete("^a-zA-Z0-9_#")

it looks like you're checking a table row for an instance of " " and
replacing with "_"
but I'm unsure of the ".delete("^a-zA-Z0-9_")"

John
--
Posted via http://www.ruby-forum.com/.