Created
January 17, 2020 07:37
-
-
Save koki-h/0b01e0a88406c8ae4483c06fdd5acf31 to your computer and use it in GitHub Desktop.
ActiveRecordモデルクラスの参照先テーブル名をモデルクラスの定義されているファイル名と同じに設定するスニペット
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
require 'tempfile' | |
require 'fileutils' | |
tables = %w| | |
table1 | |
table2 | |
table3 | |
table4 | |
table5 | |
table6 | |
table7 | |
table8 | |
table9 | |
| | |
tables.each do |t| | |
model_file = "app/models/#{t}.rb" | |
if File.exists?(model_file) | |
content = open(model_file,"r") do |f| | |
f.read | |
end | |
tf = Tempfile.create("foo") | |
content.lines.each_with_index do |line,index| | |
if index == 1 | |
tf.puts " self.table_name = '#{t}'" # モデルクラスの2行目に `self.table_name='XX'` を挿入する | |
end | |
tf.puts line | |
end | |
tf.close | |
FileUtils::mv tf.path, model_file | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment