Custom table "created_at" Ruby on Rails

I am new to Ruby on Rails, but I am wondering if it is possible to change the default name of the created_at column (an attribute used to store the date and time a new object was created) in a column named eg. "published_at" without changing its functionality.

+4
source share
2 answers

No, you can’t. The following fields are of particular importance in Rails.

  • created_at , created_on
  • updated_at , updated_on

This convention is hard-coded in several parts of the ActiveRecord database and, as in ActiveRecord 3.1, this behavior is not configurable using the settings.

If one (or more) of these fields exists, it is updated when the record is created / modified. You need to find a different name for your functionality.

+3
source

I probably would not try to change the timestamps ... In the future, you may want to implement the publication as a status. So today you can

 class YourModel alias published_at created_at end YourModel.first.published_at 

Then, in the future, you can β€œpush” publish_at to your own functionality.

+1
source

All Articles