1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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

This commit is contained in:
Pratik Naik 2010-04-02 17:58:13 +01:00
parent ee07950c03
commit 62fe16932c
2 changed files with 18 additions and 18 deletions

View file

@ -142,22 +142,6 @@ module ActiveRecord
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 file

@ -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