instance method
count
Ruby on Rails 6.0.6
Since v4.0.13Signature
count(column_name = nil, &block)
Count all records.
class Person < ActiveRecord::Base has_many :pets end # This will perform the count using SQL. person.pets.count # => 3 person.pets # => [ # #<Pet id: 1, name: "Fancy-Fancy", person_id: 1>, # #<Pet id: 2, name: "Spook", person_id: 1>, # #<Pet id: 3, name: "Choo-Choo", person_id: 1> # ]
Passing a block will select all of a person’s pets in SQL and then perform the count using Ruby.
person.pets.count { |pet| pet.name.include?('-') } # => 2
Source
# File activerecord/lib/active_record/associations/collection_proxy.rb, line 731
Defined in activerecord/lib/active_record/associations/collection_proxy.rb line 731
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::Associations::CollectionProxy