This project is archived and is in readonly mode.
before_type_cast not available for datetime fields
Reported by iros | February 16th, 2010 @ 10:39 PM
ActiveRecord version: 2.1.0
Given an ActiveRecord with the following schema:
create_table :objects do |t|
t.string :key, :null => false, :unique => true
t.datetime :start_date
end
The following sequence will result in a nil value:
o = Object.new
o.start_date = "345643456" #invalid input, might want it to be caught by a validator
o.start_date => nil
o.start_date_before_type_cast => nil
Comments and changes to this ticket
-
Thijs van der Vossen March 23rd, 2010 @ 09:10 PM
This issues also affects valid datetime values:
r = Recording.new
r.started_at = '2010-03-21T21:23:32+01:00'
r.started_at => Sun, 21 Mar 2010 20:23:32 UTC +00:00
r.started_at_before_type_cast => Sun, 21 Mar 2010 20:23:32 UTC +00:00 -
kdgundermann June 25th, 2010 @ 11:58 PM
what is expected for datetime_before_type_cast ??
- a String like "Sun, 21 Mar 2010 20:23:32 UTC +00:00" ( English time format )
- a String like "2010-03-21 20:23:32+00:00" ( another time format )
- a String like "21.03.2010 20:23:32" ( another time format for german locale )
- a String like "2010-03-21T20:23:32+00:00" ( a time format defined in ISO8601 )
- a Time object
-
Łukasz Strzałkowski July 14th, 2010 @ 09:54 PM
@kdgundermann: it's quite obvious for me - it should return the same thing that you set. It you set some FooBar class instance it should return it, etc.
I've checked this on master today and it works properly, wrote some tests for it and they pass (attached them. I think it would be good to push them to master to avoid regressions). Issue can affect only 2-3 branch. I have some problems running test on this branch, I'll check it tomorrow.
-
Akira Matsuda December 16th, 2010 @ 09:55 AM
- Tag changed from datetime, typecast to activerecord rails3, datetime, edge, patch, rails3.1, tested, typecast
I found this still is a problem on current Edge (3.1.0 beta).
So, here's a patch for the current master.
I haven't tested for 2.x yet, but am willing to work on 2.x branch as well, if you verify this patch on 3.x. -
Akira Matsuda December 19th, 2010 @ 09:58 PM
It didn't actually cleanly apply, so I created another patch for 3-0-stable branch.
Could you try this one? -
Geoff Jacobsen December 20th, 2010 @ 05:19 AM
The diff applied cleanly and looks correct but I can't run the tests as I can't get "bundle install" to work against my version of ruby (1.9.2p94) and I can't get 1.9.2p0 to install on my computer.
-
Geoff Jacobsen December 20th, 2010 @ 06:06 AM
I managed to get 1.9.2p0 installed and all the rails3-0 active_record tests passed.
-
Repository February 1st, 2011 @ 02:24 PM
- State changed from new to committed
(from [817e37013610c8e8866197594d5e408b4d5daec5]) Make before_type_cast available for datetime fields
[#3973 state:committed]
Signed-off-by: Santiago Pastorino santiago@wyeworks.com
https://github.com/rails/rails/commit/817e37013610c8e8866197594d5e4... -
Adam Meehan February 10th, 2011 @ 11:32 AM
I had my suspicions this patch was not working.
After playing around I confirmed that tests were not touching the changed code at all. This is because time_zone_aware_attributes setting was false. This meant the write method for created_at was not being generated using the timezone conversion method matcher. When switched to true the test broke.
I have attached a patch which fixes it and also cleaned up the test to make it more inline with the others test around timezone conversion.
-
Akira Matsuda February 10th, 2011 @ 11:42 AM
@Adam,
I guess we cannot upload a file to Lighthouse using Chrome or Safari. Can you try again on Firefox?
-
Adam Meehan February 10th, 2011 @ 11:44 AM
@Akira you're right I think. Driving me nuts!!
Finally, patch attached.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Referenced by
- 5126 [PATCH] Warnings about shadowing vars in AR http://github.com/strzalek/rails/compare/rails:master...m...
- 3973 before_type_cast not available for datetime fields [#3973 state:committed]
- 6489 Fix before_type_cast for timezone aware datetime attributes https://rails.lighthouseapp.com/projects/8994/tickets/39...