arel icon indicating copy to clipboard operation
arel copied to clipboard

How do I count number of records in a table

Open nadal opened this issue 14 years ago • 1 comments

Table(:users).count.to_sql is not working. What's the right way.

Looked at the test cases but could not find anything.

Thanks for all the great work.

nadal avatar Jun 17 '10 14:06 nadal

count is attached to Arel::Expression much like sum and the other aggregate methods. to use these do something similar to this (note getting arel table from ActiveRecord:

products = Product.arel_table
products.project(Arel.sql('*').count).to_sql

and you get the following output:

=> "SELECT COUNT(*) FROM [products]"

zacheryph avatar Apr 14 '11 14:04 zacheryph