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.

4 comments:

  1. Did you know the problem why you had null created_at?

    ReplyDelete
  2. Have someone understood what's the problem?

    ReplyDelete
  3. This could happen when you play with ActiveRecord::Base.record_timestamps = false.

    ReplyDelete
  4. Hey Pioz,

    You saved my day. Thanks

    ReplyDelete