Skip to content

Instantly share code, notes, and snippets.

@zeroeth
Last active December 15, 2015 13:29
Show Gist options
  • Select an option

  • Save zeroeth/5268058 to your computer and use it in GitHub Desktop.

Select an option

Save zeroeth/5268058 to your computer and use it in GitHub Desktop.
importbooks.rb
response["books"].each do |book_hash|
Book.from_json(book_hash)
end
class Book < AR
def self.from_json(book_hash)
self.new({image: book_hash["i"], whatever else})
end
end
# How to collect things 1
@list = []
response["books"].each do |book_hash|
@list << Book.from_json(book_hash)
# OR
@list.push Book.from_json(book_hash)
end
# How to collect things 2
@list = response["books"].collect do |book_hash|
Book.from_json(book_hash)
end
# How to train your dragon
@response["books"].each do |book_hash|
Book.from_json(book_hash).save
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment