开发者

why does using sum in a rails 3.1 scope result in an error?

开发者 https://www.devze.com 2023-04-04 00:52 出处:网络
I have this scope: scope :total_quantity, sum(\'quantity\') When I run: MyModel.total_quantity I get this error:

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

Runnin开发者_开发百科g 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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消