Flowdock
method

count

Importance_1
count public

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
Show source
Register or log in to add new notes.