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.

There was a problem

You must be a member of this account.

This project is archived and is in readonly mode.

ActiveRecord DateTime, Time Mappings

#1575

It appears to not be possible to call to_xml on an ActiveRecord object with various Time, Datetime columns and have it return the following:


<hour>
    <from>18:00:00</from>
    <until>22:00:00</until>
    <created-at>2008-11-05T08:28:17Z</created-at>
    <updated-at>2008-11-05T09:40:18Z</updated-at>
</hour>

Even if :from and :until were declared as time columns. Because of how ActiveRecord mixes and matches DateTime and Time class mappings (over-optimization), ActiveRecord would output the following instead:


<hour>
    <from>2000-01-01T18:00:00Z</from>
    <until>2000-01-01T22:00:00Z</until>
    <created-at>2008-11-05T08:28:17Z</created-at>
    <updated-at>2008-11-05T09:40:18Z</updated-at>
</hour>

In other words, there appears to be no way to differentiate between a time object: "14:00:00", a date object: "2008-12-15" and a datetime object: "2008-12-15 14:00:00".

At the database level, however, these differentiations are in fact honored, times are stored as times, dates as dates, and datetimes as datetimes.

So the pragmatic position is that the data treatment and the presentation treatment are different, and that the data is being misrepresented.

The world might prove to be a better place if :time were mapped to Time, :datetime to DateTime, and :date to Date. Plus dates before 1970 wouldn't give "out of range" exceptions.

The performance implications of this are of course to be considered. But so should the complexity implications. Even if the performance considerations do justify the more-complicated mapping, this should at least be done properly.

Reported by ronin-24025 (at lighthouseapp) · December 15th, 2008 @ 12:14 PM

State: stale
Milestone: 3.x
Assigned to: Frederick Cheung Frederick Cheung
Importance: none

Activity

  1. Frederick Cheung
    Frederick Cheung

    Ruby does not have a pure time of day class so :time maps to the Time class. Date columns already map to Date. As far as DateTime goes, it's a lot slower to work with than Time (since Time is just an integer offset from an epoch). These days Rails does fall back from Time to DateTime if a row contains a date not representable by Time.

    It does seem a bit broken to me that pure times of day are represented as datetimes with an arbitrary year/month/day prefix. It shouldn't be hard to change the serialization code, although I would be concerned about the backwards compatibility implications of that (it does look like someone explicitly chose to handle time columns in the same way as datetime columns though)

    December 20th, 2008 @ 01:24 PM

  2. Pratik
    Pratik
    • Assigned user set to Frederick Cheung

    March 13th, 2009 @ 11:12 AM

  3. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  4. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open
    • Importance changed from to

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 04:42 PM

  5. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:42 PM