# Inserted\_at seems buggy in production mode?

**URL:** <https://www.ruby-forum.com/t/inserted-at-seems-buggy-in-production-mode/82507>\
**Category:** Rails\
**Created:** [March 16, 2006, 12:37pm UTC](https://www.ruby-forum.com/t/inserted-at-seems-buggy-in-production-mode/82507 "2006-03-16T12:37:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [March 16, 2006, 12:37pm UTC](https://www.ruby-forum.com/t/inserted-at-seems-buggy-in-production-mode/82507/1 "2006-03-16T12:37:48Z")

</div>

Hi,

I’ve got a table with an inserted\_at column. When I insert a record it  
automatically injects the current date/time, all is well.  
Then when I insert a second record, it uses the exact same timestamp  
that is used in the first record.

This behavior only seems to occur in production mode. When I restart the  
server, the problem goes away but then immidetatly occurs again on the  
second insert. I’m using postgresql 8.1 and I have my default value for  
the inserted\_at column set to now().

Any ideas??

Jeroen

— below is some log output ----

Processing MatchController#comment (for 192.87.30.169 at 2006-03-16  
12:23:41) [POST]  
Parameters: {“mc”=\>{“body”=\>“testtest”}, “commit”=\>“Een moment …”,  
“action”=\>“comment”, “id”=\>“159”, “controller”=\>“match”}  
Match Load (0.001001) SELECT \* FROM matches WHERE (matches.id =  
‘159’) LIMIT 1  
SQL (0.000319) BEGIN  
PK and serial sequence (0.017476) SELECT attr.attname,  
name.nspname, seq.relname  
FROM pg\_class seq,  
pg\_attribute attr,  
pg\_depend dep,  
pg\_namespace name,  
pg\_constraint cons  
WHERE seq.oid = dep.objid  
AND seq.relnamespace = name.oid  
AND seq.relkind = ‘S’  
AND attr.attrelid = dep.refobjid  
AND attr.attnum = dep.refobjsubid  
AND attr.attrelid = cons.conrelid  
AND attr.attnum = cons.conkey[1]  
AND cons.contype = ‘p’  
AND dep.refobjid = ‘match\_comments’::regclass

MatchComment Create (0.002280) INSERT INTO match\_comments (“body”,  
“match\_id”, “player\_id”, “inserted\_at”) VALUES(‘testtest’, 159, 14,  
‘2006-03-16 12:22:55’)  
SQL (0.000684) SELECT currval(‘public.match\_comments\_id\_seq’)  
SQL (0.034973) COMMIT  
Redirected to [http://tcuithof.laddertennis.nl/match/show/159](http://tcuithof.laddertennis.nl/match/show/159)

Processing MatchController#comment (for 192.87.30.169 at 2006-03-16  
12:24:57) [POST]  
Parameters: {“mc”=\>{“body”=\>“test”}, “commit”=\>“Een moment …”,  
“action”=\>“comment”, “id”=\>“159”, “controller”=\>“match”}  
Match Load (0.001003) SELECT \* FROM matches WHERE (matches.id =  
‘159’) LIMIT 1  
SQL (0.000327) BEGIN  
MatchComment Create (0.000929) INSERT INTO match\_comments (“body”,  
“match\_id”, “player\_id”, “inserted\_at”) VALUES(‘test’, 159, 14,  
‘2006-03-16 12:22:55’)  
SQL (0.000448) SELECT currval(‘public.match\_comments\_id\_seq’)  
SQL (0.036623) COMMIT

Processing MatchController#comment (for 192.87.30.169 at 2006-03-16  
12:26:00) [POST]  
Parameters: {“mc”=\>{“body”=\>“dsasda”}, “commit”=\>“Een moment …”,  
“action”=\>“comment”, “id”=\>“159”, “controller”=\>“match”}  
Match Load (0.001013) SELECT \* FROM matches WHERE (matches.id =  
‘159’) LIMIT 1  
SQL (0.000307) BEGIN  
MatchComment Create (0.000991) INSERT INTO match\_comments (“body”,  
“match\_id”, “player\_id”, “inserted\_at”) VALUES(‘dsasda’, 159, 14,  
‘2006-03-16 12:22:55’)  
SQL (0.000462) SELECT currval(‘public.match\_comments\_id\_seq’)  
SQL (0.034870) COMMIT

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [March 16, 2006, 12:46pm UTC](https://www.ruby-forum.com/t/inserted-at-seems-buggy-in-production-mode/82507/2 "2006-03-16T12:46:58Z")

</div>

Jeroen H. wrote:

> Hi,
> 
> I’ve got a table with an inserted\_at column. When I insert a record it  
> automatically injects the current date/time, all is well.  
> Then when I insert a second record, it uses the exact same timestamp  
> that is used in the first record.

Okay I should have searched for this before I posted  
[http://dev.rubyonrails.org/ticket/2257](http://dev.rubyonrails.org/ticket/2257)

More logic to move out of the database. I may as well start using  
SQLite! 😉

Jeroen

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [March 16, 2006, 12:47pm UTC](https://www.ruby-forum.com/t/inserted-at-seems-buggy-in-production-mode/82507/3 "2006-03-16T12:47:06Z")

</div>

I think your problem is related to this ticket:  
[http://dev.rubyonrails.org/ticket/2257](http://dev.rubyonrails.org/ticket/2257)  
In production mode default value is cached right after the first query  
for table columns metadata, so it will be the same on subsequent  
requests.

You could rename ‘inserted\_at’ to ‘created\_at’ and use Rails magic of  
ActiveRecord::Timestamp mixin (active\_record/timestamp.rb)

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [March 16, 2006, 12:59pm UTC](https://www.ruby-forum.com/t/inserted-at-seems-buggy-in-production-mode/82507/4 "2006-03-16T12:59:39Z")

</div>

Lugovoi N. wrote:

> I think your problem is related to this ticket:  
> [http://dev.rubyonrails.org/ticket/2257](http://dev.rubyonrails.org/ticket/2257)  
> In production mode default value is cached right after the first query  
> for table columns metadata, so it will be the same on subsequent  
> requests.
> 
> You could rename ‘inserted\_at’ to ‘created\_at’ and use Rails magic of  
> ActiveRecord::Timestamp mixin (active\_record/timestamp.rb)

Thanks!

Aha I thought it should be called inserted\_at, but created\_at is the  
special column name. My bad!

So if I do that, what should I use as my column definition? NOT NULL?  
And no default value?

Jeroen
