Skip to content

Instantly share code, notes, and snippets.

@duderman
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duderman/9163651f4e366ac34196 to your computer and use it in GitHub Desktop.
Save duderman/9163651f4e366ac34196 to your computer and use it in GitHub Desktop.
Loads supplier products from csv file
def import_supplier_products(path)
fail unless File.file? path
supplier_mappings = {
'224' => '372bd2a4-258d-48f3-9f84-598e1f09b657',
'445' => '0235c6b5-b399-40ad-91f3-cbe7b02ca565'
}
imported, failed = [], []
CSV.foreach(path) do |row|
manufacturer = Registry::Manufacturer.find_or_create_by(name: row[3])
sp = SupplierProduct.new(
supplier_id: supplier_mappings[row[0]],
code: row[1],
name: row[2],
manufacturer_id: manufacturer.id
)
if sp.save
imported << sp
else
failed << row
end
end
CSV.open('/home/hq/failed_imports.csv', 'wb') do |csv|
failed.each { |f| csv << f }
end unless failed.empty?
[imported.count, failed.count]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment