Last active
August 29, 2015 14:14
-
-
Save duderman/9163651f4e366ac34196 to your computer and use it in GitHub Desktop.
Loads supplier products from csv file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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