8

I have this scope:

scope :total_quantity, sum('quantity')

When I run:

MyModel.total_quantity

I get this error:

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

Running the sum method directly works

MyModel.sum('quantity') # 4

I can't find any documentation on the default_scoped? method, or why it is being called here. Do you know if there is a way to fix this problem?

2 Answers 2

12

Just try method instead of scope . It works like charm i also faced a same problem , but when i changed my scope to method its works fine . Below is working and tested code :)

def self.total_quantity
 sum('quantity')
end

Let me know if it works or not ! Thanks

2
  • 11
    Scope-compatible methods return a scoped object, not a number as is the case with sum. You need to use this approach with things that return a final value that can't be scoped further.
    – tadman
    Sep 9, 2011 at 21:08
  • I also had the same issue where I tried to find count of objects via scope. Now its clear, thanks tadman
    – Abhi
    Jul 14, 2014 at 8:04
-1

This worked for me

def self.total_quantity
 sum('quantity')
end

But I did not get reason for an error if I uses scope instead of method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.