Allow comparison on model objects - Closes #1858 #1860

merged 2 commits into from Jun 28, 2011


None yet
4 participants

dmathieu commented Jun 25, 2011

ActiveRecord object can't be sorted because they can't be compared, as reported in #1858.
This fixes it.

It does the comparison on the object's id, because that's the only field which will be available anyway in all models.
If required to sort on an other field, it should be provided in the block option of the sort method.


samuelkadolph commented Jun 25, 2011

You need to return nil when the other_object is a different class. Thing.first <=> Blah.first should give nil.

gucki commented Jun 25, 2011

@dmathieu as far as I know the primary key does not have to be named "id". may be better use #to_key defined in activemodel (


dmathieu commented Jun 26, 2011

Thank you, I've taken both of your comments into account.

@tenderlove tenderlove added a commit that referenced this pull request Jun 28, 2011

@tenderlove tenderlove Merge pull request #1860 from dmathieu/comparison
Allow comparison on model objects - Closes #1858

@tenderlove tenderlove merged commit a5cb5a5 into rails:master Jun 28, 2011

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