# Problem with sum and include

**URL:** <https://www.ruby-forum.com/t/problem-with-sum-and-include/87918>\
**Category:** Rails\
**Created:** [March 6, 2007, 10:01pm UTC](https://www.ruby-forum.com/t/problem-with-sum-and-include/87918 "2007-03-06T22:01:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![gr0k](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e19adc/32.png) [@gr0k](https://www.ruby-forum.com/u/gr0k)\
**Post date:** [March 6, 2007, 10:01pm UTC](https://www.ruby-forum.com/t/problem-with-sum-and-include/87918/1 "2007-03-06T22:01:17Z")

</div>

Given these models:

class Intranet \< ActiveRecord::Base  
has\_many :matters  
has\_many :bills  
end

class Matter \< ActiveRecord::Base  
has\_many :bills  
belongs\_to :intranet  
end

class Bill \< Entry  
belongs\_to :intranet  
belongs\_to :matter  
end

These three queries work:

Bill.count(:include =\> :matter, :conditions =\> [“matters.number=10711”])

Bill.sum(:amount, :include =\> :matter, :conditions =\>  
[“matters.number=10711”])

Intranet.find(1).bills.sum(:amount, :include =\> :matter, :conditions =\>  
[“matters.number=10711”])

However, this does not work:

Intranet.find(1).bills.count(:include =\> :matter, :conditions =\>  
[“matters.number=10711”])

ActiveRecord::StatementInvalid: Mysql::Error: Unknown table ‘matters’ in  
where clause: SELECT count(\*) AS count\_all FROM entries WHERE  
(entries.intranet\_id = 1 AND (matters.number=10711)) AND (  
(entries.`type` = ‘Bill’ ) )

It’s ignoring the include. Am I doing something wrong or is this a bug?
