diff options
author | Francesco Rodriguez <lrodriguezsanc@gmail.com> | 2012-05-18 01:03:40 -0500 |
---|---|---|
committer | Francesco Rodriguez <lrodriguezsanc@gmail.com> | 2012-05-18 01:03:40 -0500 |
commit | d029d50d48aa90655877749a57f316f6063fccf8 (patch) | |
tree | b9d1abd67a91dd2e1af5f5f1cf48cd7f66b81c76 /activerecord/lib | |
parent | 10d375efaa85cbfab11def8ddff7069b18da7064 (diff) | |
download | rails-d029d50d48aa90655877749a57f316f6063fccf8.tar.gz rails-d029d50d48aa90655877749a57f316f6063fccf8.tar.bz2 rails-d029d50d48aa90655877749a57f316f6063fccf8.zip |
add docs to CollectionAssociation#any?
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/collection_association.rb | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index bfc2058a2c..24718cd4b1 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -305,6 +305,36 @@ module ActiveRecord size.zero? end + # Returns true if the collections is not empty. + # Equivalent to +!collection.empty?+. + # + # class Person < ActiveRecord::Base + # has_many :pets + # end + # + # person.pets.count # => 0 + # person.pets.any? # => false + # + # person.pets << Pet.new(name: 'Snoop') + # person.pets.count # => 0 + # person.pets.any? # => true + # + # Also, you can pass a block to define a criteria. The behaviour + # is the same, it returns true if the collection based on the + # criteria is not empty. + # + # person.pets + # # => [#<Pet name: "Snoop", group: "dogs">] + # + # person.pets.any? do |pet| + # pet.group == 'cats' + # end + # # => false + # + # person.pets.any? do |pet| + # pet.group == 'dogs' + # end + # # => true def any? if block_given? load_target.any? { |*block_args| yield(*block_args) } |