Created
February 19, 2016 09:42
-
-
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 :)
This file contains 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
#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