Skip to content

Instantly share code, notes, and snippets.

@chuchuva
Created May 8, 2021 23:25
Show Gist options
  • Save chuchuva/81a255d123196c6ec518de43a7a2f3ac to your computer and use it in GitHub Desktop.
Save chuchuva/81a255d123196c6ec518de43a7a2f3ac to your computer and use it in GitHub Desktop.
How to import walks from JSON file
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