views:

38

answers:

2

When I save an apartment record in rails, for some reason it is updating the updated_on field and the created_on field. I can't figure out how/why the created_on field is being updated.

To try and debug the situation, I opened up a console window and did the following

t = Apartment.find(11619)
t.beds = 4
t.save

When I check my log file, I see the following

   SQL (0.0ms)   BEGIN
   Apartment Load (0.0ms)   SELECT `apartments`.id FROM `apartments` WHERE (`apartments`.`unit_num` = BINARY '2005' AND `apartments`.building_id = 17755 AND `apartments`.id <> 11619) LIMIT 1
   Apartment Load (0.0ms)   SELECT * FROM `apartments` WHERE (`apartments`.`id` = 11619) 
   Apartment Update (0.0ms)   UPDATE `apartments` SET `beds` = 4.0, `updated_on` = '2010-07-31 19:54:10' WHERE `id` = 11619
   SQL (0.0ms)   COMMIT

This shows only the updated_on field being changed!

So I guess the question is, what could change my created_on value and yet not show up in the rails log file?

EDIT: I know the created_on field is changing because when I do a sql query to check, the value has been updated to be the same as the updated_on field. Also, I display the created_on value to my users, which is what brought this bug to my attention.

EDIT2: The table definition is as follows. I seemed to have solved my problem by changing the default value from "CURRENT_TIMESTAMP" to "0000-00-00 00:00:00", though I don't understand why this would make a difference?

Field   Type    Null    Key Default Extra
id  int(10) unsigned    NO  PRI \N  auto_increment
building_id int(10) unsigned    YES MUL 0   
unit_num    varchar(45) YES         
beds    float   YES MUL \N  
rent    int(10) unsigned    YES MUL 0   
date_available  date    YES     \N  
on_market   tinyint(1)  YES     0   
floor   varchar(10) YES     \N  
fee_paid    int(10) YES     0   
note    blob    YES     \N  
created_by  int(10) unsigned    YES     1   
updated_by  int(10) unsigned    YES     1   
created_on  timestamp   NO      CURRENT_TIMESTAMP   
updated_on  timestamp   NO      0000-00-00 00:00:00 
lease_expiration_date   date    YES     \N  
on_market_date  date    YES     \N  
off_market_date date    YES     \N  
tenant_name varchar(40) YES     \N  
tenant_number   varchar(40) YES     \N  
baths   float   YES     1   
public_note blob    YES     \N  
public_title    text    YES     \N  
key_note    blob    YES     \N  
status  varchar(40) YES     Off Market  
app_pending tinyint(1)  YES     0   
app_pending_date    date    YES     \N  
A: 

Why don't you use created_at/updated_at (rails will handle these for you)?

A discussion in Ruby Forum regarding this back in 2008 is here

It seems better to use DATETIME to do these kind of things in Rails.

Ed
created_on/updated_on are treated the same way as created_at/updated_at, rails is updating my fields currently
Janak
A: 

Based on the suggestion by Chris Henry, I took a look at my table definition and changed the default value from "CURRENT_TIMESTAMP" to "0000-00-00 00:00:00". This fixed the issue, though I don't understand why this would make a difference in my case

Janak