Skip to content

Instantly share code, notes, and snippets.

@pix0r
Created May 16, 2014 14:13
Show Gist options
  • Save pix0r/7ce214b703d9b5a824ae to your computer and use it in GitHub Desktop.
Save pix0r/7ce214b703d9b5a824ae to your computer and use it in GitHub Desktop.
def all_models
Rails.application.eager_load!
ActiveRecord::Base.descendants
end
def replace_string_across_all_objects(old, new)
all_models().each do |model|
print "Checking #{model} models\n"
model.all.each do |o|
o.attributes.each_pair do |name, value|
begin
if value.index(old) != nil
print "Updating field #{name} of #{model} id #{o.id}\n"
o[name] = value.sub old, new
o.save
end
rescue NoMethodError
end
end
end
end
end
if ARGV.length == 3
replace_string_across_all_objects(ARGV[1], ARGV[2])
else
print "Usage: #{ARGV[0]} <search> <replace>\n"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment