From 75d71017fdf16f62c7d537788a1a175546457950 Mon Sep 17 00:00:00 2001 From: Francesco Rodriguez Date: Sat, 19 May 2012 08:15:27 -0500 Subject: add CollectionProxy#first documentation --- .../active_record/associations/collection_proxy.rb | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'activerecord/lib') 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 @@ -36,6 +36,33 @@ module ActiveRecord class CollectionProxy < Relation 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 + # # => [ + # # #, + # # #, + # # # + # + # person.pets.first # => # + # person.pets.first(2) + # # => [ + # # #, + # # # + # # ] + # + # 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 -- cgit v1.2.3