views:

93

answers:

1

Hi Guys,

I have a error that I can't find the solution.

When I run:

Week.find(1).results.sum('box')

A get a SUM of column box an it works perfect!

But when I apply a filter in it:

Week.find(1).results.find(:all, :joins => [:seller],:conditions => ['sellers.user_id = ?', 1]).sum('caixas')

I get a error NoMethodError: undefined method '+' for #<Result:0x103239e58>

The object returned is the same I print It on console and don't see anything wrong.

Somebody knows something about it?

Tks!

+1  A: 

ActiveRecord#sum is an ActiveRecord method.

The first case works because Week.find(1).results returns an association proxy that exposes the same methods of Week class.

In the second case you are calling #sum on an Array object, not an ActiveRecord model. If you want the second case to work, you should use a scope or an association proxy.

Change

Week.find(1).results.find(:all, :joins => [:seller],:conditions => ['sellers.user_id = ?', 1]).sum('caixas')

to

Week.find(1).results.scoped(:joins => [:seller], :conditions => ['sellers.user_id = ?', 1]).sum('caixas')
Simone Carletti