class RuboCop::Cop::Rails::CreateTableWithTimestamps
end
t.integer :article_id
t.integer :user_id
create_table :users, articles, id: false do |t|
# good
end
t.datetime :updated_at, default: -> { ‘CURRENT_TIMESTAMP’ }
t.string :email
t.string :name
create_table :users do |t|
# good
end
t.datetime :created_at, default: -> { ‘CURRENT_TIMESTAMP’ }
t.string :email
t.string :name
create_table :users do |t|
# good
end
t.timestamps
t.string :email
t.string :name
create_table :users do |t|
# good
end
t.string :email
t.string :name
create_table :users do |t|
# bad
create_table :users
# bad
@example
user’s editing intentions.
NOTE: Allow ‘timestamps` not written when `id: false` because this emphasizes respecting
In many cases, timestamps are useful information and should be added.
Checks the migration for which timestamps are not included when creating a new table.
def on_send(node)
def on_send(node) return if !node.command?(:create_table) || use_id_false_option?(node) parent = node.parent if create_table_with_block?(parent) add_offense(parent) if parent.body.nil? || !time_columns_included?(parent.body) elsif create_table_with_timestamps_proc?(node) # nothing to do else add_offense(node) end end
def time_columns_included?(node)
def time_columns_included?(node) timestamps_included?(node) || created_at_or_updated_at_included?(node) end