Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 Modifying updated_at

I am creating a CourseEnrollment model and whenever a user views the course, I want to update the database with the current timestamp. Should I just update the existing updated_at field or create a seperate field for the model?

like image 763
Chris Muench Avatar asked Mar 08 '11 16:03

Chris Muench


2 Answers

Check out the AR touch method http://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-touch

like image 73
Wizard of Ogz Avatar answered Oct 21 '22 16:10

Wizard of Ogz


I'd create a separate field. updated_at doesn't really correctly communicate its purpose. Something like last_viewed_at would be more appropriate. Of course, updated_at will be updated when you update last_viewed_at, so you need to find a way to override that when all you want to do is update that column.

You can temporarily disable timestamp updating by setting self.record_timestamps to false in a before_filter and then re-enabling it in an after_filter.

like image 5
ryeguy Avatar answered Oct 21 '22 16:10

ryeguy