Friday, February 18, 2011

Problem with Rails, created_at or updated_at sometimes becoming null

On my Rails web application, records are sometimes saved with null created_at or updated_at.

This problem occurred for models which have child models.
Therefore I added before_create method in the model.
def before_create
   self.created_at = Time.now if self.created_at.blank?
   self.updated_at = Time.now if self.updated_at.blank?
end
This method will fix the problem.