Skip to content

Instantly share code, notes, and snippets.

@kristjan
Last active August 29, 2015 13:59
Show Gist options
  • Save kristjan/10501922 to your computer and use it in GitHub Desktop.
Save kristjan/10501922 to your computer and use it in GitHub Desktop.
# 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