arel
arel copied to clipboard
How do I count number of records in a table
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.
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]"