Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use timestamps in Rails?

Ruby on Rails' ORM has a t.timestamps handler. It adds (and fills and updates) created_at/updated_at rows in your tables.

But I can think of some reasons when not to use timestamps:

  • They clutter your database with information that might never be used.
  • They take up resources when updating/inserting items.

Are there good rules of thumb on when to include updated_at and created_at columns and when to leave them out?

Are my reasons not to use them valid? Or can that be ignored and should I simply go with "just add them, regardless, you never know when you might need them"?

like image 792
berkes Avatar asked May 16 '11 10:05

berkes


People also ask

Should all tables have timestamp?

Answers. Yes, your accessement regarding timestamp is correct and you should have it in all tabels in SQL especially if you have Access front end. This will eliminate a possible problem with Access that another user has changed or modified a row since you have opened the record. >>

How do I get a timestamp in Ruby?

The proper way is to do a Time. now. getutc. to_i to get the proper timestamp amount as simply displaying the integer need not always be same as the utc timestamp due to time zone differences.


1 Answers

Youre absolutely right. If you dont need them, dont use them.

Well we use them in our scenario - several apps talk to each other using REST. It gives us possibility to compare two resources just by comparing the timestamps. We dont need to fetch whole resource and compare by attribute basic. This is my case when I need timestamps.

I keep them turned off when I dont need them.

like image 192
lzap Avatar answered Oct 15 '22 04:10

lzap