Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Rails Code Snippet on Creating Objects from CSV data
csv_text ='lib', 'seeds', 'products.csv'))
csv = CSV.parse(csv_text, :headers => true, :encoding => 'ISO-8859-1')
csv.each do |row|
t = = 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'] = 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['']
puts "#{t.street}, #{} saved"
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.