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 8449085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:36:21+00:00 2026-06-10T10:36:21+00:00

My entry model has many counters: class Entry < ActiveRecord::Base has_many :counters end Every

  • 0

My entry model has many counters:

class Entry < ActiveRecord::Base
  has_many :counters
end

Every counter has a number, and the total represents the sum of the numbers:

class Counter < ActiveRecord::Base
  scope :total, sum(:number)
end

I need to get the sum of all the numbers of the counters which belong to a specific entry.

In SQL it would be:

SELECT SUM(`number`) AS `total` FROM `counters` WHERE `entry_id` = entry.id

I tried:

entry.counters.total

But it returns:

NoMethodError: undefined method `default_scoped?’ for 0:Fixnum

Is there any “Rails way” to do this nicely with ActiveRecord associations and scopes?

  • 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. Editorial Team
    Editorial Team
    2026-06-10T10:36:23+00:00Added an answer on June 10, 2026 at 10:36 am

    In your example the call to sum happens straightaway, ie your code is equivalent to

    scope :total, 0
    

    (assuming that the sum is 0 at the moment that your class is 0), which isn’t valid

    Fundamentally scopes are about scoping a result set : adding conditions, order, limit or options such as joins, but with the constant that the result is a collection of active record objects.

    What you want to do is best expressed as a class method:

    def self.total
      sum(:number)
    end
    

    You can still chain this onto a scope, for example

    some_entry.counters.total
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this rails model association (has_many :through) class User < ActiveRecord::Base has_many :assignments
Im using Rails 3.0.3 I have a Model Bill: class Bill < ActiveRecord::Base has_many
I have three models named Metric, Entry, and Measurement: class Metric < ActiveRecord::Base has_many
I have an Entry model which has many Tag s. Tag s are added
I have the following models: class FieldEntryValue < ActiveRecord::Base belongs_to :field_entry end and class
For the following models: class Entity(models.Model): name = models.CharField(max_length=256) class Entry(models.Model): A <subj> has
Say that Blog model has a one-to-many relationship with Entry model. In a form,
In my Review model, I have the following: class Review < ActiveRecord::Base belongs_to :vendor
class Blog(models.Model): name = models.CharField(max_length=100) class Entry(models.Model): blog = models.ForeignKey(Blog) headline = models.CharField(max_length=255) Is
I have a such model named Foo: class Foo(models.Model): name = models.CharField() entry =

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.