Guest User

Untitled

a guest
Apr 24th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. # -- Export
  2.  
  3. require 'csv'
  4. CSV.open('books.csv', 'w') do |file|
  5. Book.all.each {|b|
  6. file << [
  7. b.isbn, b.sku_number, b.title,
  8. b.author_family, b.author_given, b.author, '',
  9. b.edition, b.description, b.sale_price,
  10. b.cover.try(:full_filename),
  11. b.category, b.featured_content
  12. ]
  13. }
  14. end
  15.  
  16. # -- Import
  17.  
  18. require 'csv'
  19. CSV.open('/var/apps/catawba/current/books.csv', 'r') do |b|
  20. begin
  21. book = Book.new({
  22. :isbn => b[0],
  23. :sku => b[1],
  24. :title => b[2],
  25. :author_given => b[3],
  26. :author_family => b[4],
  27. :author_fn => b[5],
  28. :author_email => b[6],
  29. :edition => b[7],
  30. :description => b[8],
  31. :price => b[9]
  32. })
  33. begin
  34. book.cover = File.open(b[10])
  35. rescue Exception => e
  36. puts = e
  37. end
  38. book.save!
  39. book.categories << Category.find_by_name(b[11])
  40. book.categories << Category.find_by_name("Featured Books") if b[12] =~ /true/
  41. rescue Exception => e
  42. puts b[0] +" - "+ e
  43. end
  44. end
Add Comment
Please, Sign In to add comment