Skip to content

Instantly share code, notes, and snippets.

@inkmix
Last active August 27, 2015 11:29
Show Gist options
  • Save inkmix/3153c21d34f6087b5e8a to your computer and use it in GitHub Desktop.
Save inkmix/3153c21d34f6087b5e8a to your computer and use it in GitHub Desktop.
country = 'nl'
wdays = [3,5,6]
nl_zipcodes = CSV.read('zipcodes_nl.csv').map(&:first)
existing_zipcodes = DeliveryZip.for_country(country).pluck(:zip)
added_zips = nl_zipcodes - existing_zipcodes
existing_zips_not_in_file = existing_zipcodes - nl_zipcodes
ActiveRecord::Base.transaction do
wdays.each do |wday|
slot_ids = DeliverySlot.for_country(country).joins(:delivery_slot_week_days).
where(delivery_slot_week_days: {wday: wday}).pluck(:id).uniq
nl_zipcodes.each do |zipcode|
delivery_zip = DeliveryZip.find_or_create_by(zip: zipcode, country: country)
slot_ids.each do |slot_id|
DeliveryZipDeliverySlot.find_or_create_by(delivery_zip_id: delivery_zip.id, delivery_slot_id: slot_id)
DeliverySlotWeekDay.find_or_create_by(delivery_zip_id: delivery_zip.id, delivery_slot_id: slot_id, wday: wday)
end
end
end
end
puts "zipcodes that were not in the system yet: #{added_zips}"
puts "zipcodes that are in the system but not in the file #{existing_zips_not_in_file}"
@valakirka
Copy link

LGTM

@simaofreitas
Copy link

slots = DeliverySlot.for_country(country).joins(:delivery_slot_week_days).where(:'delivery_slot_week_days.wday' => wdays).uniq

could be

slots = DeliverySlot.for_country(country).joins(:delivery_slot_week_days).where(delivery_slot_week_days: {wday: wdays}).uniq

But it's just cosmetic :)

LGTM

@inkmix
Copy link
Author

inkmix commented Aug 27, 2015

ahh! I always forget about this one!! nice

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