From c74045cf0771ab51dfeca94b30c447cab6193e60 Mon Sep 17 00:00:00 2001 From: Brian Christian Date: Tue, 9 Feb 2016 16:02:08 -0800 Subject: allow Array.penultimate and Array.antepenultiate access methods --- .../lib/active_record/relation/finder_methods.rb | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'activerecord/lib/active_record/relation/finder_methods.rb') diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index d48bcea28a..90e05dc340 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -242,6 +242,38 @@ module ActiveRecord find_nth! 41 end + # Find the third-to-last record. + # If no order is defined it will order by primary key. + # + # Person.antepenultimate # returns the third-to-last object fetched by SELECT * FROM people + # Person.offset(3).antepenultimate # returns the third-to-last object from OFFSET 3 + # Person.where(["user_name = :u", { u: user_name }]).antepenultimate + def antepenultimate + find_nth -3 + end + + # Same as #antepenultimate but raises ActiveRecord::RecordNotFound if no record + # is found. + def antepenultimate! + find_nth! -3 + end + + # Find the second-to-last record. + # If no order is defined it will order by primary key. + # + # Person.penultimate # returns the second-to-last object fetched by SELECT * FROM people + # Person.offset(3).penultimate # returns the second-to-last object from OFFSET 3 + # Person.where(["user_name = :u", { u: user_name }]).penultimate + def penultimate + find_nth -2 + end + + # Same as #penultimate but raises ActiveRecord::RecordNotFound if no record + # is found. + def penultimate! + find_nth! -2 + end + # Returns true if a record exists in the table that matches the +id+ or # conditions given, or false otherwise. The argument can take six forms: # -- cgit v1.2.3