diff options
Diffstat (limited to 'activerecord/lib/active_record/associations/collection_association.rb')
-rw-r--r-- | activerecord/lib/active_record/associations/collection_association.rb | 74 |
1 files changed, 34 insertions, 40 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index 6e5877f6f5..7c688d663c 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -111,47 +111,47 @@ module ActiveRecord end end - def first(*args) - first_nth_or_last(:first, *args) + def first(limit = nil) + find_nth_or_last(:first, limit) end - def second(*args) - first_nth_or_last(:second, *args) + def second + find_nth_or_last(:second) end - def third(*args) - first_nth_or_last(:third, *args) + def third + find_nth_or_last(:third) end - def fourth(*args) - first_nth_or_last(:fourth, *args) + def fourth + find_nth_or_last(:fourth) end - def fifth(*args) - first_nth_or_last(:fifth, *args) + def fifth + find_nth_or_last(:fifth) end - def forty_two(*args) - first_nth_or_last(:forty_two, *args) + def forty_two + find_nth_or_last(:forty_two) end - def third_to_last(*args) - first_nth_or_last(:third_to_last, *args) + def third_to_last + find_nth_or_last(:third_to_last) end - def second_to_last(*args) - first_nth_or_last(:second_to_last, *args) + def second_to_last + find_nth_or_last(:second_to_last) end - def last(*args) - first_nth_or_last(:last, *args) + def last(limit = nil) + find_nth_or_last(:last, limit) end - def take(n = nil) - if loaded? - n ? target.take(n) : target.first + def take(limit = nil) + if find_from_target? + limit ? load_target.take(limit) : load_target.first else - scope.take(n) + scope.take(limit) end end @@ -221,11 +221,11 @@ module ActiveRecord dependent = if dependent dependent - elsif options[:dependent] == :destroy - :delete_all - else - options[:dependent] - end + elsif options[:dependent] == :destroy + :delete_all + else + options[:dependent] + end delete_or_nullify_all_records(dependent).tap do reset @@ -608,14 +608,10 @@ module ActiveRecord # * target already loaded # * owner is new record # * target contains new or changed record(s) - def fetch_first_nth_or_last_using_find?(args) - if args.first.is_a?(Hash) - true - else - !(loaded? || - owner.new_record? || - target.any? { |record| record.new_record? || record.changed? }) - end + def find_from_target? + loaded? || + owner.new_record? || + target.any? { |record| record.new_record? || record.changed? } end def include_in_memory?(record) @@ -646,11 +642,9 @@ module ActiveRecord end # Fetches the first/last using SQL if possible, otherwise from the target array. - def first_nth_or_last(type, *args) - args.shift if args.first.is_a?(Hash) && args.first.empty? - - collection = fetch_first_nth_or_last_using_find?(args) ? scope : load_target - collection.send(type, *args) + def find_nth_or_last(ordinal, limit = nil) + collection = find_from_target? ? load_target : scope + limit ? collection.send(ordinal, limit) : collection.send(ordinal) end end end |