Modified recorded variables created_at property

Is there a way to change the database record "created_at" after creating it?

+4
source share
2 answers

Of course, since rails 2.3 you can change it like any other column: get the object from the database, set the value for the column, save it.

my_model = MyModel.find(42) my_model.created_at = Date.today my_model.save 
+5
source

If you want to keep the creation date, you may not want to use the default mark.

Use the before_save filter and the 'creation_date' custom field

 class MyModel before_save :set_creation_date def set_creation_date self.creation_date ||= Date.today end end 

Thus, if you set create_date yourself, it will be saved in db with this value, but otherwise Date.today will be.

0
source

Source: https://habr.com/ru/post/1341337/