This project is archived and is in readonly mode.

#4124 ✓resolved
Daniele Orlandi

ActiveRecord range condition with Time objects produces unquoted date strings in SQL

Reported by Daniele Orlandi | March 7th, 2010 @ 03:30 PM | in 3.0.2

Rails 3.0.0.beta, the DBMS is PostgreSQL.
I don't know if this bug is related to ActiveRecord or PostgreSQL adaptor, however this is the issue:

I have a model "Flight" with a timestamp :takeoff_time column.

Normal range selects with numeric parameters work fine:

Flight.where(:pilot_id => 1..10).count SQL (11.2ms) SELECT COUNT(*) AS count_id FROM "flights" WHERE ("flights"."pilot_id" BETWEEN 1 AND 10)

Equal comparison with Time work fine:

Flight.where(:takeoff_time => Time::now).count SQL (0.5ms) SELECT COUNT(*) AS count_id FROM "flights" WHERE ("flights"."takeoff_time" = '2010-03-07 15:52:41.248325')

Ranges with Time with ... inclusion work fine:

Flight.where(:takeoff_time => Time::now...Time::now).count SQL (0.4ms) SELECT COUNT(*) AS count_id FROM "flights" WHERE ("flights"."takeoff_time" >= '2010-03-07 15:54:05.438399') AND ("flights"."takeoff_time" < '2010-03-07 15:54:05.438400')

.. ranges produce this:

Flight.where(:takeoff_time => Time::now..Time::now).count PGError: ERROR: syntax error at or near "16"
LINE 1: ... ("flights"."takeoff_time" BETWEEN 2010-03-07 16:53:53 +...


: SELECT COUNT(*) AS count_id FROM "flights" WHERE ("flights"."takeoff_time" BETWEEN 2010-03-07 16:53:53 +0100 AND 2010-03-07 16:53:53 +0100)

Comments and changes to this ticket

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=""></a>