Allow comparison on model objects - Closes #1858 #1860

Merged
merged 2 commits into from Jun 28, 2011

Conversation

Projects
None yet
4 participants
Contributor

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.

Contributor

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 (http://api.rubyonrails.org/classes/ActiveModel/Conversion.html#method-i-to_key)?

Contributor

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
a5cb5a5

@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