From bc743dc1ce4657be0c377edaab69f8e9ca0e350b Mon Sep 17 00:00:00 2001 From: Damien Mathieu <42@dmathieu.com> Date: Sat, 25 Jun 2011 16:35:59 +0200 Subject: allow comparison on model objects - Closes #1858 --- activerecord/lib/active_record/base.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'activerecord/lib/active_record') 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: -- cgit v1.2.3 From eb22c51173f3ebe0f5a53f8bc57ebc74e4f0a824 Mon Sep 17 00:00:00 2001 From: Damien Mathieu <42@dmathieu.com> Date: Sun, 26 Jun 2011 09:41:30 +0200 Subject: comparing different classes returns nil --- activerecord/lib/active_record/base.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index a2b5a3ec4f..e970445082 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1806,7 +1806,11 @@ MSG # Allows sort on objects def <=>(other_object) - self.to_key <=> other_object.to_key + if other_object.is_a?(self.class) + self.to_key <=> other_object.to_key + else + nil + end end # Backport dup from 1.9 so that initialize_dup() gets called -- cgit v1.2.3