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
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
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
···
On Feb 2, 2010, at 1:22 PM, John Mcleod 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.
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) )
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
···
On Feb 2, 2010, at 2:19 PM, John Mcleod 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.