Created
October 4, 2017 21:27
-
-
Save pixeltrix/b197fa5d0bed86a9c5b9ac99ed43bfc0 to your computer and use it in GitHub Desktop.
Migration to add timestamps easily to a bunch of tables
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
class AddTimestampsEverywhere < ActiveRecord::Migration[5.1] | |
def change | |
now = Time.current | |
%i[ | |
candidates | |
evaluations | |
interviewers | |
questions | |
responses | |
stages | |
workflows | |
].each do |table| | |
change_table table do |t| | |
t.timestamps null: true | |
end | |
klass = Class.new(ActiveRecord::Base) do | |
self.table_name = table | |
end | |
klass.update_all(created_at: now, updated_at: now) | |
change_column_null(table, :created_at, false) | |
change_column_null(table, :updated_at, false) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment