1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #21332 from ronakjangir47/take_docs

Added docs for CollectionProxy#take
This commit is contained in:
Eileen M. Uchitelle 2015-08-23 11:12:35 -04:00
commit c106ba9f8d

View file

@ -227,6 +227,31 @@ module ActiveRecord
@association.last(*args)
end
# Gives a record (or N records if a parameter is supplied) from the collection
# using the same rules as <tt>ActiveRecord::Base.take</tt>.
#
# 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.take # => #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>
#
# person.pets.take(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.take # => nil
# another_person_without.pets.take(2) # => []
def take(n = nil)
@association.take(n)
end