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.

ActiveRecord::Base minimum and maximum methods do not return datetimes in local timezone

#1514

Whenever you do a minimum or maximum call on a datetime field of a has-many collection of ActiveRecord objects, the resulting datetime is not converted into the local timezone.

This is not consistent behaviour since ActiveRecord will always convert the datetime fields into the local timezone otherwise. (even if you use find_by_sql)

Example:

Database is set to store datetimes in UTC

Local timezone is set to EST

Milestone model has many Ticket objects

Ticket model has a created_at datetime field

milestone.tickets.minimum(:created_at) will return a datetime in UTC zone

Reported by James Ho · December 3rd, 2008 @ 04:27 PM

State: wontfix
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Geoff Buesing
    Geoff Buesing
    • State changed from new to wontfix

    Hmm I think the current behavior actually makes sense here -- the time zone conversions are done with custom attribute reader and writer methods; i.e., they read and write values to the attributes hash. A calculation doesn't involve the attributes hash at all.

    Similarly, if you created a custom reader method for, say, a length method, that read_attribute(:length) and converted from meters to feet or whatever, this conversion wouldn't be invoked for a :minimum calculation, either -- you'd get back the minimum value in whatever scale the db stored it in.

    February 8th, 2009 @ 06:08 PM

  2. Sean Kirby
    Sean Kirby

    If the behaviour isn't going to get changed, how about a clarification in the docs that this method is going directly to the db? That would at least make this behaviour more obvious.

    February 11th, 2009 @ 05:00 PM