Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8309 (closed defect: untested)

Opened 1 year ago

Last modified 10 months ago

NoMethod error in postgresl_adapter.rb. Usec is not a method for DateTime Class.

Reported by: henkez Assigned to: bitsweat
Priority: normal Milestone: 1.2.4
Component: ActiveRecord Version: 1.2.3
Severity: normal Keywords: postgresql datetime quoting
Cc:

Description

I get the following error after upgrading to 1.2.3. NoMethodError (undefined method `usec' for #<DateTime: 212045465540169721/86400000000,0,2299161>):

/var/lib/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/postgresql_adapter.rb:579:in `cast_to_time'

If you look at the code you can see that usec is added in the 1.2 versions.

1.2 Stable

576	        def cast_to_time(value)
577	          return value unless value.class == DateTime
578	          v = value
579	          time_array = [v.year, v.month, v.day, v.hour, v.min, v.sec, v.usec]
580	          Time.send(Base.default_timezone, *time_array) rescue nil
581	        end

Which wasn't there in 1.1 1.1 Stable

499        def cast_to_time(value)
500	          return value unless value.class == DateTime
501	          v = value
502	          time_array = [v.year, v.month, v.day, v.hour, v.min, v.sec]
503	          Time.send(Base.default_timezone, *time_array) rescue nil
504	        end

As far as I know DateTime class doesn't have a usec method.

Change History

06/05/07 04:46:39 changed by bitsweat

  • keywords set to postgresql datetime quoting.
  • owner changed from core to bitsweat.

06/05/07 04:47:01 changed by bitsweat

  • status changed from new to closed.
  • resolution set to untested.

Do you have a fix and failing unit test?

06/07/07 09:35:14 changed by henkez

  • status changed from closed to reopened.
  • resolution deleted.

Do you need unit tests to prove that there is no usec method of DateTime class? Read the ruby docs.

DateTime class doesn't have an usec method. Time class have an usec method.

The fix is easy. Remove the v.usec from time_array if you are planning to return a DateTime class.

06/07/07 09:46:56 changed by bitsweat

  • priority changed from high to normal.
  • status changed from reopened to closed.
  • resolution set to untested.
  • severity changed from major to normal.

We use unit tests to verify that Rails' behavior is correct. In this case, that it doesn't raise a NoMethodError for DateTime#usec.

The idea is: 1. Write the test and see it fail, exhibiting the error above. 2. Apply your patch fixing the error. 3. Run the test and see it pass, demonstrating that the patch works and preventing future regression.

So add a test, update the patch, and it'll move forward. Thanks!

07/23/07 02:38:05 changed by mpalmer

henkez,

I'm having trouble working out how to get the PostgreSQL connection adapter to call cast_to_time with a DateTime object. Can you give me a snippet of code/database that fails for you, so I can incorporate it into a unit test?

08/29/07 10:27:15 changed by henkez

mpalmer,

I think you need to change PGconn.translate_results = true in the postgresql_adapter.rb. I've had to do that for the last 10 rails versions.