aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2011-01-24 22:47:18 +0000
committerJon Leighton <j@jonathanleighton.com>2011-01-30 11:58:08 +0000
commitca7785847eafaf687b10c7757dc034d08d1e6ba8 (patch)
treed5e667a04592ef72e46bd4b2a3cd42ae2b8be82a
parentfdee153ff8cde2d44d751f8f961e2df13a80cd5c (diff)
downloadrails-ca7785847eafaf687b10c7757dc034d08d1e6ba8.tar.gz
rails-ca7785847eafaf687b10c7757dc034d08d1e6ba8.tar.bz2
rails-ca7785847eafaf687b10c7757dc034d08d1e6ba8.zip
Condense first_or_last a bit more
-rw-r--r--activerecord/lib/active_record/associations/association_collection.rb12
1 files changed, 5 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb
index 586965bb3b..7151f53fdb 100644
--- a/activerecord/lib/active_record/associations/association_collection.rb
+++ b/activerecord/lib/active_record/associations/association_collection.rb
@@ -498,7 +498,7 @@ module ActiveRecord
# * target contains new or changed record(s)
# * the first arg is an integer (which indicates the number of records to be returned)
def fetch_first_or_last_using_find?(args)
- if args.first.kind_of?(Hash) && !args.first.empty?
+ if args.first.is_a?(Hash)
true
else
!(loaded? ||
@@ -536,12 +536,10 @@ module ActiveRecord
# Fetches the first/last using SQL if possible, otherwise from the target array.
def first_or_last(type, *args)
- if fetch_first_or_last_using_find?(args)
- scoped.send(type, *args)
- else
- args.shift if args.first.kind_of?(Hash) && args.first.empty?
- load_target.send(type, *args)
- end
+ args.shift if args.first.is_a?(Hash) && args.first.empty?
+
+ collection = fetch_first_or_last_using_find?(args) ? scoped : load_target
+ collection.send(type, *args)
end
end
end