AR models tell they are comparable (have a <=>) but in fact they are not: #1858

Closed
gucki opened this Issue Jun 25, 2011 · 7 comments

Comments

Projects
None yet
5 participants

gucki commented Jun 25, 2011

I found this while running my specs. For a full description of the problem please have a look here rspec/rspec-core#413 and here rspec/rspec-expectations#80

Contributor

dchelimsky commented Jun 25, 2011

Here's a script to reproduce:

rails new example
cd example
rails generate model thing name:string
rake db:migrate
rails console

Now, in the console:

> [Thing.create!, Thing.create!].sort
....
ArgumentError: comparison of Thing with Thing failed
    from (irb):1:in `sort'
....

gucki commented Jun 25, 2011

@dchelimsky thanks for the code snippet, excellent :)

Member

vijaydev commented Jun 25, 2011

I'm getting a NoMethodError: undefined method `<=>' for #Thing:0x5453210 on both edge and 3.1.0.rc4.

Contributor

dchelimsky commented Jun 25, 2011

Try it in ruby 1.9.2 :)

Contributor

dchelimsky commented Jun 25, 2011

To be clear, I get NoMethodError: undefined method '<=>' for #Thing with 3.1.0.rc4 on ruby 1.8.7, but I get the ArgumentError on ruby 1.9.2.

Contributor

dchelimsky commented Jun 25, 2011

ArgumentError also on edge + ruby 1.9.2

Contributor

dmathieu commented Jun 25, 2011

The error is actually the same for 1.8 and 1.9.
ActiveRecord::Base needs to respond to <=>.

I've fixed it by sorting by id in #1860.

@ghost ghost assigned tenderlove Jun 25, 2011

guilleiguaran pushed a commit to guilleiguaran/rails that referenced this issue Jun 28, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment