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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:11:09+00:00 2026-06-07T23:11:09+00:00

I am having what I think is a simple problem chaining two scopes (Rails

  • 0

I am having what I think is a simple problem chaining two scopes (Rails 3.2.5).

I have a model called Point with fields amount and transaction_date, amongst other. Users get an amount of points for various activities, and they are “available” until used, which happens as part of a transaction, at which point transaction_date is updated, and no longer empty.

So I have a scope like this:

scope :available, where("transaction_date IS NULL OR transaction_date = ''")

and it works great, returning the proper collection of Point objects. So I can do

> Point.available
=> [#<Point id: 123, amount: 22, transaction_date: nil >][#<Point id: 456, amount: 33, transaction_date: nil >]

And if I want to know the sum of available points, I can do

> Point.available.sum("amount")
=> 55

But if I try to make another scope like

scope :available, where("transaction_date IS NULL OR transaction_date = ''")
scope :total_available, available.sum("amount")

I get the error

NoMethodError: undefined method `default_scoped?' for 22:Fixnum

or if I change the scope so it’s sum("amount").available I get the error

NoMethodError: undefined method `available' for 55:Fixnum

I can also make the :total_available scope work by adding the condition defined in :available, but that’s not very DRY.

What am I missing here?

  • 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-07T23:11:11+00:00Added an answer on June 7, 2026 at 11:11 pm

    The expression available.sum gets evaluate straight away so your attempts are equivalent to

    scope :total_available, 55
    

    Or

    scope :total_available, 55.available
    

    Which is clearly wrong. Personally I’d use a class method for this

    def self.total_available
      available.sum(:amount)
    end
    

    I think of scopes as a scoped collection: it doesn’t make sense to me for scopes to be numbers – for one thing they’d be scopes you couldn’t chain other scoped onto

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

Sidebar

Related Questions

I have what I think is a very simple problem (famous last words)... I
I think this is simple, but I am having big problem with this. I
I have this little problem and I think I am having it due to
I have a simple problem that I'm having trouble thinking around: var oldValues :
I have created a simple JSF image browsing app, and I'm having a problem
I am having a bit of a problem here. I have two int values,
I’ve got what I think is a simple problem but I’m having a tough
Alright, this is a weird problem I'm having. I have two pages, which are
I'm having an issue that I think is simple to diagnose, but I can't
I think this should be simple but I am having some difficulty implementing it.

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.