rails db:seedをした時に毎回テーブルの中身を初期化する処理を書く。
code
ActiveRecord::Base.establish_connection
ActiveRecord::Base.connection.disable_referential_integrity do
ActiveRecord::Base.connection.tables.each do |table|
next if table == 'schema_migrations'
ActiveRecord::Base.connection.execute("TRUNCATE TABLE `#{table}`")
end
end
table周りの処理で `を書くと、予約語にバッティングしているテーブルがあっても削除できる。 sqlの処理としてぶつからない。