Class: RuboCop::Cop::DevDoc::Migration::RequireTimestamps
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::DevDoc::Migration::RequireTimestamps
- Extended by:
- AutoCorrector
- Defined in:
- lib/rubocop/cop/dev_doc/migration/require_timestamps.rb
Overview
Every ‘create_table` migration must include `t.timestamps`.
## Rationale Every table should have ‘created_at` and `updated_at` columns —there is no reason to omit them. They are essential for debugging, auditing, and ordering records, and adding them later is much more painful than including them up front.
This rule applies to every ‘create_table` call, including those with `id: false`. If a genuinely timestamp-less table is needed, add an explicit `# rubocop:disable` with a rationale comment.
❌
create_table :ai_api_failures do |t|
t.belongs_to :ai_chat_message, null: false, foreign_key: true
t.string :error_type
t.datetime :created_at
end
✔️
create_table :ai_api_failures do |t|
t.belongs_to :ai_chat_message, null: false, foreign_key: true
t.string :error_type
t.
end
Constant Summary collapse
- MSG =
'Add `t.timestamps` to this `create_table` migration.'.freeze
- RESTRICT_ON_SEND =
%i[create_table].freeze
Instance Method Summary collapse
Instance Method Details
#on_send(node) ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/rubocop/cop/dev_doc/migration/require_timestamps.rb', line 58 def on_send(node) block = node.parent return unless block&.block_type? return if (block.body) add_offense(node.loc.selector) do |corrector| autocorrect(corrector, block) end end |