Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 95903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:43:52+00:00 2026-05-10T23:43:52+00:00

Rails gurus: I’ve just discovered named_scope thanks to another SO user. :) I’d like

  • 0

Rails gurus: I’ve just discovered named_scope thanks to another SO user. 🙂

I’d like to get the count of a set of rows – i.e. a SELECT COUNT(*). Additionally, I want to still be able to chain named scopes in the call.

Is this a legitimate (albeit weird) usage of named scope?

named_scope :count, :select => 'COUNT(*) as count_all' 

So then I can do (for example):

@foobar = Foobar.count.scope.scope.scope 

The count is accessed via @foobar.first.count_all.

(Edited to address Allan’s comments)

You could do:

@foobar = Foobar.scope.scope.scope.size 

But this would cause a result query and not the faster SELECT COUNT(*) query. I have a large amount of rows in the database I am querying.

Is there a better way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T23:43:52+00:00Added an answer on May 10, 2026 at 11:43 pm

    The functionality you’re looking for is built in.

    Foobar.count # SELECT count(*) AS count_all FROM 'foobars' Foobar.named_scope.count # SELECT count(*) AS count_all FROM 'foobars' WHERE .... 

    If you run script/server in dev mode, you’ll see the queries as they get executed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Rails offers helpers like 24.hours , 1.minute etc. How can I get the number
[Rails] Where to put this code? I have a user1 and when another registered
Rails: I need to set a condition in a has_many that needs the self.id
Rails 3.1 I'll simplify my application to get at my question. I have two
rails 3.1 rake routes for admin/sections_contoller trigger_admin_section GET /admin/sections/:id/trigger(.:format) {:action=>trigger, :controller=>admin/sections} admin_sections GET /admin/sections(.:format)
Rails newbie here so just want to be sure I'm doing this right. I
Greetings Rails and Javascript Gurus! I have a project where I am returning a
In the past I've written sites in ASP.NET just to get nice reusable templating
Rails doesn't seem to be loading the Kaminari gem, as I get an Uninitialized
Rails likes to get column attributes for each table you're using. On MySQL this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.