Post.count to_sql in Rails3?

I have a Post model name. When I try to start Post.count I get the result without any problems. However, I suspect that Ruby is used to count the number of messages returned.

Instead, I would like to use SQL to count the number of messages, as this is much faster.

The only thing I found for this using Arel is Post.select("COUNT(id)") . Can't run the count command without explicitly calling select on the model? Thanks!

+4
source share
2 answers

Is this what you want?

 Post.where(your_sql).count 

Read here

+3
source

Post.count should generate a request:

 SELECT COUNT(*) FROM "posts" 

Edit: you can see the requests generated when viewing your development.log file.

+12
source

Source: https://habr.com/ru/post/1333971/


All Articles