This project is archived and is in readonly mode.

#1514 ✓wontfix
James Ho

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

Reported by James Ho | December 3rd, 2008 @ 04:27 PM | in 2.x

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

Comments and changes to this ticket

  • Geoff Buesing

    Geoff Buesing February 8th, 2009 @ 06:08 PM

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

  • Sean Kirby

    Sean Kirby February 11th, 2009 @ 05:00 PM

    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.

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