Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow comparison on model objects - Closes #1858 #1860

Merged
merged 2 commits into from
Jun 28, 2011
Merged

Allow comparison on model objects - Closes #1858 #1860

merged 2 commits into from
Jun 28, 2011

Conversation

dmathieu
Copy link
Contributor

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
Copy link
Contributor

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

@gucki
Copy link

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)?

@dmathieu
Copy link
Contributor Author

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

tenderlove added a commit that referenced this pull request Jun 28, 2011
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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants