Skip to content

Instantly share code, notes, and snippets.

@sha1sum
Created October 7, 2015 19:30
Show Gist options
  • Save sha1sum/6893eb178addd4a4d93c to your computer and use it in GitHub Desktop.
Save sha1sum/6893eb178addd4a4d93c to your computer and use it in GitHub Desktop.
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