Skip to content
Browse files

Make Relation#first and Relation#last behave like named scope's

  • Loading branch information...
1 parent ee07950 commit 62fe16932c9b7c3044017900114193e06814fd0c @lifo lifo committed Apr 2, 2010
View
16 activerecord/lib/active_record/named_scope.rb
@@ -142,22 +142,6 @@ def self.init(klass, options, &block)
relation.merge(scope)
end
- def first(*args)
- if args.first.kind_of?(Integer) || (loaded? && !args.first.kind_of?(Hash))
- to_a.first(*args)
- else
- args.first.present? ? apply_finder_options(args.first).first : super
- end
- end
-
- def last(*args)
- if args.first.kind_of?(Integer) || (loaded? && !args.first.kind_of?(Hash))
- to_a.last(*args)
- else
- args.first.present? ? apply_finder_options(args.first).last : super
- end
- end
-
def ==(other)
case other
when Scope
View
20 activerecord/lib/active_record/relation/finder_methods.rb
@@ -106,13 +106,29 @@ def find(*args, &block)
# 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

0 comments on commit 62fe169

Please sign in to comment.
Something went wrong with that request. Please try again.