Why does using a sum in rails 3.1 cause an error?

I have this area:

scope :total_quantity, sum('quantity') 

When I run:

 MyModel.total_quantity 

I get this error:

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

The sum method works directly

 MyModel.sum('quantity') # 4 

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

+8
ruby-on-rails
source share
2 answers

Just try the method instead of scope. It works like a charm, and I ran into the same problem, but when I changed my scope to a method, it works great. Below is a working and verified code :)

 def self.total_quantity sum('quantity') end 

Let me know if this works or not! thanks

+12
source share

It worked for me

 def self.total_quantity sum('quantity') end 

But I had no reason for the error if I use scope instead of a method.

-one
source share

All Articles