This project is archived and is in readonly mode.

#565 ✓invalid
Tomash

datetime associations work only with hour-containing input

Reported by Tomash | July 7th, 2008 @ 03:20 PM | in 2.x

I don't know how Rails create accessors based on DBs column type, but accessors for SQL datetime time act weird.

The problem: assigning string (for example from a form) to datetime value (like begins_at) works good with full ISO-format string ("YYYY-MM-DD hh:mm"), but given a string without an hour ("YYYY-MM-DD") sets the field to nil.

>> f.happened_at = "2004-05-06 11:45"
=> "2004-05-06 11:45"
>> f.happened_at
=> Thu May 06 11:45:00 +0200 2004
>> f.happened_at = "2004-05-06"
=> "2004-05-06"
>> f.happened_at
=> nil

(Which of course automatically breaks validates_date_time plugin, but this is irrelevant ATM).

The workaround: custom accessors work fine when using to_datetime on string, also on one without hours:

class CalendarEvent < ActiveRecord::Base
  def begins_at=(datetime_str)
    write_attribute(:begins_at, datetime_str.to_datetime)
  end
end

Proposed solution: make datetime field accessors preprocess the value given with .to_datetime. Works like charm for Time, Date, Datetime and "ill-formatted" (i.e. ISO without an hour) strings described above.

I'd gladly do it myself (or at least cover it with a plugin), but don't know where to dig, i.e. which method of which class & module to overload.

Comments and changes to this ticket

  • Geoff Buesing

    Geoff Buesing July 7th, 2008 @ 09:39 PM

    I can't reproduce this -- would you be able to submit a failing test case?

  • Tomash

    Tomash July 8th, 2008 @ 08:39 AM

    Tried it with a clean app and... damn I'm dumb -- the bug also doesn't occur.

    I've tracked down the bug to have its root in validates_date_time plugin which does some ugly monkeypatching and overloads rails date(time) parsing code with some crappy one of its own.

    Ticket can be closed.

    Excuse me for generating noise in Rails tickets without thorough examination first.

  • Geoff Buesing

    Geoff Buesing July 8th, 2008 @ 02:41 PM

    • State changed from “new” to “invalid”

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

Pages