0

Is there a

rails g migration AddTimestampsToModel something:something

that will generate

class AddTimestampsToModel < ActiveRecord::Migration[5.1]
  def change
    add_timestamps :model, null: false, default: -> { 'NOW()' }
  end
end

This code comes up in this topic but I don't have enough rating to comment on the response of one of the user.

Add timestamps to existing table in db Rails 5+

Answer in above link post

rails g migration AddTestToTest timestamps:timestamp null:false default: NOW()

asks for function and I am not sure what this means

Frostyfx
  • 1
  • 2
  • When creating a new model in Rails the timestamps are added by default. They are names `created_at` and `updated_at`. So I am wondering: do you want to add these default timestamps to a model that is lacking them, or do you want to create a custom timestamp like `accepted_at` or such ? – Maxence Jan 25 '23 at 09:35
  • I am looking to add them to a model that was originally created without the created_at and updated_at columns. – Frostyfx Jan 25 '23 at 15:11
  • Have you tried the favorited answer from the thread you mentionned here: https://stackoverflow.com/questions/46520907/add-timestamps-to-existing-table-in-db-rails-5 cause it seems non trivial and some coders seems to have more luck while trying different migrations syntaxes – Maxence Jan 26 '23 at 09:19
  • I have looked at the favourited answer. My question is more about having the `rails g migration` command automatically generate the right migration internals. I can generate a blank migration and manually enter in ``` class AddTimestampsToModel < ActiveRecord::Migration[5.1] def change add_timestamps :model, null: false, default: -> { 'NOW()' } end end ``` and the migration will work correctly. Just seeing if there is a shortcut per say. – Frostyfx Jan 30 '23 at 17:46

0 Answers0