Technical
Csv Loader Class
require 'csv'
class CsvLoader
def load(file)
ActiveRecord::Base.transaction do
CSV.open(file, 'r') do |row|
c = Company.new(
:company_name => row[0],
:fax_number => row[1],
:category => row[2],
:sub_category => row[3]
)
unless c.save
puts "Failed validations!!"
puts "Row: '#{row.inspect}'"
c.invalid = true
full_msgs = []
c.errors.each do |attr, msg|
full_msg = "#{attr.humanize} #{msg}"
puts "- #{full_msg}"
full_msgs << full_msg
case msg
when "can't be blank"
c.send("#{attr}=", "-")
end
end
c.reason_for_being_invalid = full_msgs.join("\n")
unless c.save
puts "Company: #{c.inspect}"
raise
end
end
end
end
puts "Total Companies: #{Company.find(:all).size}"
puts "Invalid Companies: #{Company.find_all_by_invalid(true).size}"
end
end
Created on June 10, 2007 20:47:05
by
Gregory Bluvshteyn
(24.186.106.132)
(1123 characters / 0.0 pages)