Skip to content

Instantly share code, notes, and snippets.

@Hamada92
Last active June 27, 2019 04:33
Show Gist options
  • Save Hamada92/f8fd7166e40ca21e1366cb4c483f4dc9 to your computer and use it in GitHub Desktop.
Save Hamada92/f8fd7166e40ca21e1366cb4c483f4dc9 to your computer and use it in GitHub Desktop.
#csv is the wave 1-5 files for centers.
childcare = Childcare.first
CSV.parse(csv, headers: true).each_with_index do |row, index|
center_kindercare_id = row['Location ID']
region_id = row['Location Region'].to_i
district_id = row['Location District'].to_i
label = construct_region_label(region_id: region_id, district_id: district_id)
region = Region.where(label: label).first_or_create! do |r|
r.childcare_id = childcare.id
r.kindercare_id = region_id
end
center = Center.where(kindercare_id: center_kindercare_id).first
center.update_attributes!(region_id: region.id)
end
def construct_region_label(region_id:, district_id:)
label = "R#{0 if region_id < 10}#{region_id} D#{0 if district_id < 10}#{district_id}"
end
@afrome
Copy link

afrome commented Jun 26, 2019

line 14: center = Center.where(kindercare_id: center_kindercare_id).first
in Rails4 can now do this: center = Center.find_by(kindercare_id: center_kindercare_id)

@afrome
Copy link

afrome commented Jun 26, 2019

line 15: typically would want to use update_attributes to be safe

@afrome
Copy link

afrome commented Jun 26, 2019

def construct_region_label(region_id:, district_id:) --> Should probably be in in Region model

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment