What's the right way to do counts in Rails?

You don't need a name scope to perform a count.

Account.where(:admin => false).count

But named scopes are an excellent way to make your code more reusable.

Named scopes don't have any noticeable performance impact on your application.


I would recommend you to avoid direct access to database in my templates because then you're losing a bit of flexibility when it comes to caching.

Try to prepare all the data you need to render in your action instead and then use meaningful instance variables like @number_of_accounts or @accounts.count.

This will make your views cleaner and easier to debug and also a bit more DRY if you render action in different formats (html, json, etc)

As to how do you get your numbers - it doesn't really matter that much, just move away from find_* methods towards scoping and write readable code


A named scope shouldn't have an impact on performance

scope :not_admin, where(:admin => false)

Then you can have Account.not_admin.count

Edited per DGM's comment: To check the generated SQL in a console, compare Account.not_admin.to_sql with Account.find_all_by_admin(false).to_sql