# Timestamp - automatically inserting

**URL:** <https://www.ruby-forum.com/t/timestamp-automatically-inserting/82361>\
**Category:** Rails\
**Created:** [January 19, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/timestamp-automatically-inserting/82361 "2007-01-19T16:29:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [January 19, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/timestamp-automatically-inserting/82361/1 "2007-01-19T16:29:41Z")

</div>

Hi:

I have timestamps in virtually all of my tables. The timestamp I want  
to add is always the current time, whenever I create a record in a  
table. Is this the type of thing I can be setting in my models,  
without have to be worrying about it in my controller methods. And if  
so, how would i do that in the model?

Any help you can provide would be appreciated! Thanks!

Mike

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [January 19, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/timestamp-automatically-inserting/82361/2 "2007-01-19T16:29:57Z")

</div>

If you include a datetime column called created\_at, it will  
automatically be populated. Ditto for modified\_at.

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/timestamp-automatically-inserting/82361/3 "2007-01-19T16:31:51Z")

</div>

> I have timestamps in virtually all of my tables. The timestamp I want  
> to add is always the current time, whenever I create a record in a  
> table. Is this the type of thing I can be setting in my models,  
> without have to be worrying about it in my controller methods. And if  
> so, how would i do that in the model?
> 
> Any help you can provide would be appreciated! Thanks!

If you have a field in your database table named updated\_at that is  
:datetime, rails will automatically do it for you and you don’t need to  
do  
anything else anywhere. Similarly for created\_at

I’m sure there’s a way in your model to tell Rails to use a different  
column than updated\_at, but search for updated\_at for more details.

---

<div class="post-metadata">

**Author:** ![miked](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ed655f/32.png) [@miked](https://www.ruby-forum.com/u/miked)\
**Post date:** [September 25, 2007, 10:29pm UTC](https://www.ruby-forum.com/t/timestamp-automatically-inserting/82361/4 "2007-09-25T22:29:15Z")

</div>

On Jan 12, 2007, at 1:47 PM, Miked wrote:

> Mike

> **[ActiveRecord::Timestamp](https://api.rubyonrails.org/classes/ActiveRecord/Timestamp.html)**
>
> Active Record Timestamp Active Record automatically timestamps create and update operations if the table has fields named created\_at/created\_on or updated\_at/updated\_on.

Just:  
add\_column :my\_table, :created\_at, :datetime

to each table and let ActiveRecord to the rest.

You can also use updated\_at to see modification times. If you want  
the date only, created\_on/updated\_on are the way to go.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
