Method: ActiveRecord::Associations::CollectionProxy#empty?

Defined in:
activerecord/lib/active_record/associations/collection_proxy.rb

#empty?Boolean

Returns true if the collection is empty. If the collection has been loaded it is equivalent to collection.size.zero?. If the collection has not been loaded, it is equivalent to !collection.exists?. If the collection has not already been loaded and you are going to fetch the records anyway it is better to check collection.load.empty?.

class Person < ActiveRecord::Base
  has_many :pets
end

person.pets.count  # => 1
person.pets.empty? # => false

person.pets.delete_all

person.pets.count  # => 0
person.pets.empty? # => true

Returns:

  • (Boolean)


831
832
833
# File 'activerecord/lib/active_record/associations/collection_proxy.rb', line 831

def empty?
  @association.empty?
end