Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

created_at field sometimes becomes nil

I think created_at is always set to the time a record created in by ActiveRecord, but I found some records are created with null created_at. Are there any condition to cause this?

like image 675
yaotti Avatar asked Nov 30 '11 09:11

yaotti


2 Answers

These columns created_at, updated_at, created_on, updated_on are automatically handled for you by rails.

However, there are a few notes:

  1. Do not change the attr's value (i.e. created_at should be nil before create and shouldn't be changed before update). Otherwise, ActiveRecord won't update attr's value with the current time.
  2. Check that your <ClassName>.record_timestamps is set to true.

Also, I'd suggest to you to add not-null constraint to these columns:

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

This way you will be sure that this column always have a not-null value.

like image 152
melekes Avatar answered Nov 19 '22 20:11

melekes


Are you using attr_accessible or attr_protected on your model?

Because without those basic protections, any update request could be setting these otherwise unvalidated & unprotected attributes.

Mass-assignment security

like image 44
Mars Avatar answered Nov 19 '22 19:11

Mars