diff options
author | Damien Mathieu <42@dmathieu.com> | 2011-06-25 16:35:59 +0200 |
---|---|---|
committer | Damien Mathieu <42@dmathieu.com> | 2011-06-26 09:41:08 +0200 |
commit | bc743dc1ce4657be0c377edaab69f8e9ca0e350b (patch) | |
tree | d0bf14d0803c6f57c5828f6d8eaadfb07d72045c /activerecord | |
parent | 211fa2d28d0f8a875792e078e1fca3718649de3c (diff) | |
download | rails-bc743dc1ce4657be0c377edaab69f8e9ca0e350b.tar.gz rails-bc743dc1ce4657be0c377edaab69f8e9ca0e350b.tar.bz2 rails-bc743dc1ce4657be0c377edaab69f8e9ca0e350b.zip |
allow comparison on model objects - Closes #1858
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/base.rb | 5 | ||||
-rw-r--r-- | activerecord/test/cases/base_test.rb | 7 |
2 files changed, 12 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 6a5d282973..a2b5a3ec4f 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1804,6 +1804,11 @@ MSG @attributes.frozen? end + # Allows sort on objects + def <=>(other_object) + self.to_key <=> other_object.to_key + end + # Backport dup from 1.9 so that initialize_dup() gets called unless Object.respond_to?(:initialize_dup) def dup # :nodoc: diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 2224097f04..873c6d9678 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -478,6 +478,13 @@ class BasicsTest < ActiveRecord::TestCase def test_hashing assert_equal [ Topic.find(1) ], [ Topic.find(2).topic ] & [ Topic.find(1) ] end + + def test_comparison + topic_1 = Topic.create! + topic_2 = Topic.create! + + assert_equal [topic_2, topic_1].sort, [topic_1, topic_2] + end def test_readonly_attributes assert_equal Set.new([ 'title' , 'comments_count' ]), ReadonlyTitlePost.readonly_attributes |