I have a model name Post. When I try to run Post.count I get a result without a problem. However, I suspect that Ruby is used to count the number of posts returned.
Instead, I'd like to use SQL to count the number of posts, as it is much faster.
The only way I have found to achieve this using Arel is Post.select("COUNT(id)")
.
Is there no way to run a count
command without explicitly calling select on the model?
Thanks!
Post.count should generate the query:
SELECT COUNT(*) FROM "posts"
Edit: You can see the queries generated by looking at your development.log file.
Is that what you want? :
Post.where(your_sql).count
Read here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With