diff options
author | Eileen M. Uchitelle <eileencodes@gmail.com> | 2015-08-23 11:12:35 -0400 |
---|---|---|
committer | Eileen M. Uchitelle <eileencodes@gmail.com> | 2015-08-23 11:12:35 -0400 |
commit | c106ba9f8dde72cfc856d1ecbc7b9de821483b67 (patch) | |
tree | b3a8ade541a2de3c473a24342dc9c96186ce84ca /activerecord/lib/active_record/associations | |
parent | 3cbaeb16013ef488a9619dd00dd00fe49bb6994d (diff) | |
parent | f9f156b22c77eb01c009eaac421afcb2a2d6673f (diff) | |
download | rails-c106ba9f8dde72cfc856d1ecbc7b9de821483b67.tar.gz rails-c106ba9f8dde72cfc856d1ecbc7b9de821483b67.tar.bz2 rails-c106ba9f8dde72cfc856d1ecbc7b9de821483b67.zip |
Merge pull request #21332 from ronakjangir47/take_docs
Added docs for CollectionProxy#take
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r-- | activerecord/lib/active_record/associations/collection_proxy.rb | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index b5a8c81fe4..29c711082a 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -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 |