Skip to content

Instantly share code, notes, and snippets.

@ambethia
Created November 13, 2009 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ambethia/234217 to your computer and use it in GitHub Desktop.
Save ambethia/234217 to your computer and use it in GitHub Desktop.
# -- Export
require 'csv'
CSV.open('books.csv', 'w') do |file|
Book.all.each {|b|
file << [
b.isbn, b.sku_number, b.title,
b.author_family, b.author_given, b.author, '',
b.edition, b.description, b.sale_price,
b.cover.try(:full_filename),
b.category, b.featured_content
]
}
end
# -- Import
require 'csv'
CSV.open('/var/apps/catawba/current/books.csv', 'r') do |b|
begin
book = Book.new({
:isbn => b[0],
:sku => b[1],
:title => b[2],
:author_given => b[3],
:author_family => b[4],
:author_fn => b[5],
:author_email => b[6],
:edition => b[7],
:description => b[8],
:price => b[9]
})
begin
book.cover = File.open(b[10])
rescue Exception => e
puts = e
end
book.save!
book.categories << Category.find_by_name(b[11])
book.categories << Category.find_by_name("Featured Books") if b[12] =~ /true/
rescue Exception => e
puts b[0] +" - "+ e
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment