Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

before_type_cast not available for datetime fields

#3973

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

Reported by iros · February 16th, 2010 @ 10:39 PM

State: committed
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Thijs van der Vossen
    Thijs van der Vossen

    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

    March 23rd, 2010 @ 09:10 PM

  2. David Trasbo
    David Trasbo

    Verified with Rails 2.3.5.

    April 15th, 2010 @ 07:52 PM

  3. kdgundermann
    kdgundermann

    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

    June 25th, 2010 @ 11:58 PM

  4. Łukasz Strzałkowski
    Łukasz Strzałkowski

    @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.

    July 14th, 2010 @ 09:54 PM

  5. Akira Matsuda
    Akira Matsuda
    • 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.

    December 16th, 2010 @ 09:55 AM

  6. Geoff Jacobsen
    Geoff Jacobsen

    Can this patch also be applied to the 3.0 branch?

    December 19th, 2010 @ 08:44 PM

  7. Akira Matsuda
    Akira Matsuda

    It didn't actually cleanly apply, so I created another patch for 3-0-stable branch.
    Could you try this one?

    December 19th, 2010 @ 09:58 PM

  8. Geoff Jacobsen
    Geoff Jacobsen

    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.

    December 20th, 2010 @ 05:19 AM

  9. Geoff Jacobsen
    Geoff Jacobsen

    I managed to get 1.9.2p0 installed and all the rails3-0 active_record tests passed.

    December 20th, 2010 @ 06:06 AM

  10. Repository
    Repository
    • State changed from new to committed

    February 1st, 2011 @ 02:24 PM

  11. Adam Meehan
    Adam Meehan

    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.

    February 10th, 2011 @ 11:32 AM

  12. Adam Meehan
    Adam Meehan

    The patch (bloody lighthouse!!)

    February 10th, 2011 @ 11:34 AM

  13. Akira Matsuda
    Akira Matsuda

    @Adam,

    I guess we cannot upload a file to Lighthouse using Chrome or Safari. Can you try again on Firefox?

    February 10th, 2011 @ 11:42 AM

  14. Adam Meehan
    Adam Meehan

    @Akira you're right I think. Driving me nuts!!

    Finally, patch attached.

    February 10th, 2011 @ 11:44 AM