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
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
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
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
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
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
Well, if I’d do a little detective work, I’d notice that I’m working
with Hashes. Therefore, the “Hash” class.
John