aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorFrancesco Rodriguez <lrodriguezsanc@gmail.com>2012-05-19 08:15:27 -0500
committerFrancesco Rodriguez <lrodriguezsanc@gmail.com>2012-05-19 08:15:27 -0500
commit75d71017fdf16f62c7d537788a1a175546457950 (patch)
treedfc74b2a9fb82c4ca22c4971355ed731608cfa19 /activerecord
parentd0a3be308c141ddebf86a8cf7dcb360965c57d36 (diff)
downloadrails-75d71017fdf16f62c7d537788a1a175546457950.tar.gz
rails-75d71017fdf16f62c7d537788a1a175546457950.tar.bz2
rails-75d71017fdf16f62c7d537788a1a175546457950.zip
add CollectionProxy#first documentation
Diffstat (limited to 'activerecord')
-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 03cbba8dea..401019e1f3 100644
--- a/activerecord/lib/active_record/associations/collection_proxy.rb
+++ b/activerecord/lib/active_record/associations/collection_proxy.rb
@@ -37,6 +37,33 @@ module ActiveRecord
delegate :target, :load_target, :loaded?, :to => :@association
##
+ # :method: first
+ # Returns the first record, or the first +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.first # => #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>
+ # person.pets.first(2)
+ # # => [
+ # # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>,
+ # # #<Pet id: 2, name: "Spook", person_id: 1>
+ # # ]
+ #
+ # another_person_without.pets # => []
+ # another_person_without.pets.first # => nil
+ # another_person_without.pets.first(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