From 62fe16932c9b7c3044017900114193e06814fd0c Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Fri, 2 Apr 2010 17:58:13 +0100 Subject: Make Relation#first and Relation#last behave like named scope's --- .../lib/active_record/relation/finder_methods.rb | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'activerecord/lib/active_record/relation') diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index 37aaac0894..a26f1c0ac8 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -106,13 +106,29 @@ module ActiveRecord # A convenience wrapper for find(:first, *args). You can pass in all the # same arguments to this method as you can to find(:first). def first(*args) - args.any? ? apply_finder_options(args.first).first : find_first + if args.any? + if args.first.kind_of?(Integer) || (loaded? && !args.first.kind_of?(Hash)) + to_a.first(*args) + else + apply_finder_options(args.first).first + end + else + find_first + end end # A convenience wrapper for find(:last, *args). You can pass in all the # same arguments to this method as you can to find(:last). def last(*args) - args.any? ? apply_finder_options(args.first).last : find_last + if args.any? + if args.first.kind_of?(Integer) || (loaded? && !args.first.kind_of?(Hash)) + to_a.last(*args) + else + apply_finder_options(args.first).last + end + else + find_last + end end # A convenience wrapper for find(:all, *args). You can pass in all the -- cgit v1.2.3