instance method
any?
Ruby on Rails 4.1.16
Since v4.0.13Signature
any?(&block)
Returns true if the collection is not 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
You can also pass a block to define criteria. The behavior 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
Parameters
-
blockblock
Source
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 811
def any?(&block)
@association.any?(&block)
end
Defined in activerecord/lib/active_record/associations/collection_proxy.rb line 811
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::Associations::CollectionProxy