Skip to content

Instantly share code, notes, and snippets.

@slavone
Created February 19, 2016 09:42
Show Gist options
  • Save slavone/d72af8c229ce9e30fc18 to your computer and use it in GitHub Desktop.
Save slavone/d72af8c229ce9e30fc18 to your computer and use it in GitHub Desktop.
ruby script to add dependent: :destroy to all models if you forgot to do so in the first place :)
#path to folder with model files
dir = "./app/models/*.rb"
Dir.glob dir do |file|
pattern = /(has_one|has_many).*$/
text_to_append = ", dependent: :destroy\n"
before = File.read file
new_text = ''
before.each_line do |line|
if match = line.match(pattern)
line = line.chomp + text_to_append
end
new_text += line
end
File.open(file, 'w') { |f| f << new_text }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment