Created
October 7, 2015 19:30
-
-
Save sha1sum/6893eb178addd4a4d93c to your computer and use it in GitHub Desktop.
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
class Admin::Product < ActiveRecord::Base | |
require 'csv' | |
has_attached_file :image, | |
styles: { medium: "500x500>", | |
thumb: "200x200>" }, | |
default_url: "/images/:style/missing.jpg" | |
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/ | |
def self.import(file) | |
CSV.foreach(file.path, headers: true) do |row| | |
product_hash = row.to_hash | |
product = Admin::Product.find_by(id: product_hash["id"]) | |
if product | |
product.update product_hash | |
else | |
Admin::Product.create! product_hash | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment