Skip to content

Instantly share code, notes, and snippets.

@code-later
Created November 24, 2011 16:51
Show Gist options
  • Save code-later/1391787 to your computer and use it in GitHub Desktop.
Save code-later/1391787 to your computer and use it in GitHub Desktop.
Apply arbitrary changes to a bunch of ActiveRecord entities
changes = {
"Boooking:234" => lambda { |record| record.some_attribute = "new_value" }
}
changes.each do |model_class_and_id, change|
begin
klass = model_class_and_id.split(":").first.constantize
id = model_class_and_id.split(":").last.to_i
print "== Changing #{klass} '#{id}' ... "
record = klass.find(id)
change.call(record)
print "Changes were: #{record.changes.inspect} "
if ENV["SAVE"]
record.save(false)
print "[DONE]"
end
rescue => e
print "[FAILED] (#{e.message})"
end
puts
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment