Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rails Code Snippet on Creating Objects from CSV data
csv_text = File.read(Rails.root.join('lib', 'seeds', 'products.csv'))
csv = CSV.parse(csv_text, :headers => true, :encoding => 'ISO-8859-1')
csv.each do |row|
t = Product.new
t.id = row['id']
t.title = row['title']
t.brand = row['brand']
t.description = row['description']
t.main_image = row['picture']
t.condition = row['condition']
t.category = row['category']
t.style = row['style']
t.price = row['price']
t.retail_price = row['retailprice']
t.sale_price = row['sale price']
t.is_on_sale = row['is on sale']
t.user_id = row['user']
t.created_at = row['created_at']
t.updated_at = row['updated_at']
t.approved_at = row['approved_at']
t.size = row['size']
t.color = row['color']
t.wear_tags = row['wear_tags']
t.images = row['']
t.sold_at=row['sold_at']
t.slug=row['']
t.save
puts "#{t.street}, #{t.city} saved"
end
puts "There are now #{Product.count} rows in the products table"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.