Created
May 16, 2014 14:13
-
-
Save pix0r/7ce214b703d9b5a824ae 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
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