Forum: Ruby on Rails record count

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
frank (Guest)
on 2006-12-22 07:19
I have a query that selects all records from a db.  In the view i loop
through the records.

Does rails setup a variable that holds the amount of records the query
returned?  Similar to Coldfusions recordcount?
gaurav bagga (Guest)
on 2006-12-22 08:18
(Received via mailing list)
hi,

from doc

Examples for counting all:

  Person.count         # returns the total count of all people

Examples for count by conditions and joins (this has been deprecated):

  Person.count("age > 26")  # returns the number of people older than 26
  Person.find("age > 26 AND job.salary > 60000", "LEFT JOIN jobs on
jobs.person_id = person.id") # returns the total number of rows
matching the conditions and joins fetched by SELECT COUNT(*).

Examples for count with options:

  Person.count(:conditions => "age > 26")
  Person.count(:conditions => "age > 26 AND job.salary > 60000",
:include => :job) # because of the named association, it finds the
DISTINCT count using LEFT OUTER JOIN.
  Person.count(:conditions => "age > 26 AND job.salary > 60000",
:joins => "LEFT JOIN jobs on jobs.person_id = person.id") # finds the
number of rows matching the conditions and joins.
  Person.count('id', :conditions => "age > 26") # Performs a COUNT(id)
  Person.count(:all, :conditions => "age > 26") # Performs a COUNT(*)
(:all is an alias for '*')

Note: Person.count(:all) will not work because it will use :all as the
condition. Use Person.count instead.


might help

regards

gaurav
Aaron (Guest)
on 2006-12-22 09:04
(Received via mailing list)
On Dec 21, 11:16 pm, "gaurav bagga" <removed_email_address@domain.invalid> 
wrote:
> Examples for counting all:
>
>   Person.count         # returns the total count of all people

If you only want the counts then the above method is considerably
faster.  If you want the records AND the count then just use "length".
ActiveRecord finds return an array, and array.length will tell you how
many records were returned.

people = Person.find(:all)
puts "#{people.length} records returned"
people.each {|person| puts person.name}

Aaron
frank (Guest)
on 2006-12-22 18:43
Aaron wrote:
> On Dec 21, 11:16 pm, "gaurav bagga" <removed_email_address@domain.invalid> wrote:
>> Examples for counting all:
>>
>>   Person.count         # returns the total count of all people
>
> If you only want the counts then the above method is considerably
> faster.  If you want the records AND the count then just use "length".
> ActiveRecord finds return an array, and array.length will tell you how
> many records were returned.
>
> people = Person.find(:all)
> puts "#{people.length} records returned"
> people.each {|person| puts person.name}
>
> Aaron

Length is what I was looking for, thank you.
This topic is locked and can not be replied to.