aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/finder_methods.rb
diff options
context:
space:
mode:
authorBrian Christian <brchristian@gmail.com>2016-02-09 16:02:08 -0800
committerBrian Christian <brchristian@gmail.com>2016-02-09 16:02:08 -0800
commitc74045cf0771ab51dfeca94b30c447cab6193e60 (patch)
tree277b16c5e586866a7284b63ac753035a7f81dd3e /activerecord/lib/active_record/relation/finder_methods.rb
parentb5eb2423b6e431ba53e3836d58449e7e810096b4 (diff)
downloadrails-c74045cf0771ab51dfeca94b30c447cab6193e60.tar.gz
rails-c74045cf0771ab51dfeca94b30c447cab6193e60.tar.bz2
rails-c74045cf0771ab51dfeca94b30c447cab6193e60.zip
allow Array.penultimate and Array.antepenultiate access methods
Diffstat (limited to 'activerecord/lib/active_record/relation/finder_methods.rb')
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb32
1 files changed, 32 insertions, 0 deletions
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:
#