Created
May 8, 2021 23:25
-
-
Save chuchuva/81a255d123196c6ec518de43a7a2f3ac to your computer and use it in GitHub Desktop.
How to import walks from JSON file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Walk < ApplicationRecord | |
has_many :photos | |
def self.import | |
file = File.read('./walks-in-victoria.json') | |
walks = JSON.parse(file) | |
walks.each do |w| | |
walk = Walk.create(title: w['title'], grade: w['grade'], | |
grade_note: w['gradeNote'], encoded_line: w['encodedLine']) | |
w['images'].each do |image_url| | |
walk.photos.create(url: image_url) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment