Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Closed
gucki opened this Issue · 7 comments

5 participants

@gucki

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

@dchelimsky

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

@dchelimsky thanks for the code snippet, excellent :)

@vijaydev
Collaborator

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

@dchelimsky

Try it in ruby 1.9.2 :)

@dchelimsky

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.

@dchelimsky

ArgumentError also on edge + ruby 1.9.2

@dmathieu dmathieu referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
@dmathieu
Collaborator

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.

@tenderlove tenderlove was assigned
@guilleiguaran guilleiguaran referenced this issue from a commit in guilleiguaran/rails
@dmathieu dmathieu allow comparison on model objects - Closes #1858 c5b43af
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.