Reputation: 2367
Is there a way to change a database records "created_at" variable after it created?
Upvotes: 5
Views: 6500
Reputation: 79
If you want to store a creation date, you may not want to use the default timestamp behavior.
Use a before_save filter, and a custom field 'creation_date'
class MyModel
before_save :set_creation_date
def set_creation_date
self.creation_date ||= Date.today
end
end
That way, if you set creation_date yourself, it will get saved to the db with that value, but otherwise Date.today will.
Upvotes: 0
Reputation: 370132
Sure, 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
Upvotes: 5