aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorFrancesco Rodriguez <lrodriguezsanc@gmail.com>2012-05-19 08:20:49 -0500
committerFrancesco Rodriguez <lrodriguezsanc@gmail.com>2012-05-19 08:20:49 -0500
commitb0f55c68887930ada279302cceaf10e2ca67de52 (patch)
tree156470e36f9fb13558c007b3118bca0013bb7a11 /activerecord/lib/active_record
parent75d71017fdf16f62c7d537788a1a175546457950 (diff)
downloadrails-b0f55c68887930ada279302cceaf10e2ca67de52.tar.gz
rails-b0f55c68887930ada279302cceaf10e2ca67de52.tar.bz2
rails-b0f55c68887930ada279302cceaf10e2ca67de52.zip
add CollectionProxy#last documentation
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/associations/collection_proxy.rb27
1 files changed, 27 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb
index 401019e1f3..a7d7e3a870 100644
--- a/activerecord/lib/active_record/associations/collection_proxy.rb
+++ b/activerecord/lib/active_record/associations/collection_proxy.rb
@@ -64,6 +64,33 @@ module ActiveRecord
# another_person_without.pets.first(3) # => []
##
+ # :method: last
+ # Returns the last record, or the last +n+ records, from the collection.
+ # If the collection is empty, the first form returns nil, and the second
+ # form returns an empty array.
+ #
+ # class Person < ActiveRecord::Base
+ # has_many :pets
+ # end
+ #
+ # person.pets
+ # # => [
+ # # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
+ #
+ # person.pets.last # => #<Pet id: 3, name: "Choo-Choo", person_id: 1>
+ # person.pets.last(2)
+ # # => [
+ # # #<Pet id: 2, name: "Spook", person_id: 1>,
+ # # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
+ # # ]
+ #
+ # another_person_without.pets # => []
+ # another_person_without.pets.last # => nil
+ # another_person_without.pets.last(3) # => []
+
+ ##
# :method: concat
# Add one or more records to the collection by setting their foreign keys
# to the association's primary key. Since << flattens its argument list and