aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorFrancesco Rodriguez <lrodriguezsanc@gmail.com>2012-05-18 01:03:40 -0500
committerFrancesco Rodriguez <lrodriguezsanc@gmail.com>2012-05-18 01:03:40 -0500
commitd029d50d48aa90655877749a57f316f6063fccf8 (patch)
treeb9d1abd67a91dd2e1af5f5f1cf48cd7f66b81c76 /activerecord
parent10d375efaa85cbfab11def8ddff7069b18da7064 (diff)
downloadrails-d029d50d48aa90655877749a57f316f6063fccf8.tar.gz
rails-d029d50d48aa90655877749a57f316f6063fccf8.tar.bz2
rails-d029d50d48aa90655877749a57f316f6063fccf8.zip
add docs to CollectionAssociation#any?
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/associations/collection_association.rb30
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) }