aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2010-04-02 17:58:13 +0100
committerPratik Naik <pratiknaik@gmail.com>2010-04-02 18:57:46 +0100
commit62fe16932c9b7c3044017900114193e06814fd0c (patch)
tree97df93c52da5573aff7509fae5557d3c955d7a0d /activerecord/lib/active_record/relation
parentee07950c03bf8aab703191d73165eb206f9f55ef (diff)
downloadrails-62fe16932c9b7c3044017900114193e06814fd0c.tar.gz
rails-62fe16932c9b7c3044017900114193e06814fd0c.tar.bz2
rails-62fe16932c9b7c3044017900114193e06814fd0c.zip
Make Relation#first and Relation#last behave like named scope's
Diffstat (limited to 'activerecord/lib/active_record/relation')
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb20
1 files changed, 18 insertions, 2 deletions
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 <tt>find(:first, *args)</tt>. You can pass in all the
# same arguments to this method as you can to <tt>find(:first)</tt>.
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 <tt>find(:last, *args)</tt>. You can pass in all the
# same arguments to this method as you can to <tt>find(:last)</tt>.
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 <tt>find(:all, *args)</tt>. You can pass in all the