Last active
August 29, 2015 13:59
-
-
Save kristjan/10501922 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Data looks like: | |
# { | |
# #"old": new, | |
# "7583": 201343, | |
# "7614": 280397, | |
# "8381": 285146, | |
# "9197": 285727, | |
# "9356": 220591, | |
# } | |
def update_rental_ids(klass, old, new) | |
rel = klass.where("rental_id like 'p#{old}-%'") | |
rel.each do |thing| | |
begin | |
new_rid = thing.rental_id.sub("p#{old}-", "p#{new}-") | |
new_attrs = { | |
rental_id: new_rid, | |
} | |
new_attrs[:property_id] = new if thing.respond_to?(:property_id) | |
puts "Saving #{thing.class.name} #{thing.id}" | |
thing.update_attributes!(new_attrs) | |
rescue ActiveRecord::RecordNotUnique | |
thing.destroy | |
end | |
end | |
end | |
data.each_with_index do |(old, new), i| | |
puts "\n\n---#{i}---\n\n" | |
[ | |
Unit, | |
UserUnit.includes(:user), | |
SavedSearchListing.includes(:saved_searches), | |
SpamFlag, | |
Community | |
].each do |klass| | |
puts "Updating #{klass.name}" | |
update_rental_ids(klass, old, new) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment