Permalink
Browse files

add docs to CollectionAssociation#any?

  • Loading branch information...
Francesco Rodriguez
Francesco Rodriguez committed May 18, 2012
1 parent 10d375e commit d029d50d48aa90655877749a57f316f6063fccf8
Showing with 30 additions and 0 deletions.
  1. +30 −0 activerecord/lib/active_record/associations/collection_association.rb
@@ -305,6 +305,36 @@ def empty?
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) }

0 comments on commit d029d50

Please sign in to comment.