Created_at sometimes becomes nil

I think that created_at always set for the time a record was created by ActiveRecord, but I found that some records are created with a created_at value of zero. Is there any condition to cause this?

+8
ruby-on-rails activerecord
source share
3 answers

These columns created_at, updated_at, created_on, updated_on are automatically processed by you using rails.

However, there are a few notes:

  • Do not change the attr value (i.e., created_at must be nil before creation and must not be changed before the update ). Otherwise, ActiveRecord will not update the attr value with the current time.
  • Verify that <ClassName>.record_timestamps set to true.

In addition, I suggest that you add a non-zero constraint to these columns:

 change_column :<table_name>, :created_at, :datetime, :null => false 

This way you will be sure that this column always has a value other than zero.

+1
source share

Are you attr_accessible or attr_protected in your model?

Because without these basic protections, any update request can set these otherwise unapproved and insecure attributes.

Mass Assignment Security

+1
source share

The only thing I could think of is that maybe you are rewriting the created_at attribute in your view.

 <%= t.text_field :created_at %> 

And the value that you pass when submitting the form is not converted using ActiveRecord, becoming nil .
created_at not intended for management; it is better to create another field of type creation_date and leave it by default. But this is just an assumption.

0
source share

All Articles