# ActiveRecord problems from within template

**URL:** <https://www.ruby-forum.com/t/activerecord-problems-from-within-template/94963>\
**Category:** Rails\
**Created:** [May 18, 2006, 7:04am UTC](https://www.ruby-forum.com/t/activerecord-problems-from-within-template/94963 "2006-05-18T07:04:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tachekent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7993a0/32.png) [@tachekent](https://www.ruby-forum.com/u/tachekent)\
**Post date:** [May 18, 2006, 7:04am UTC](https://www.ruby-forum.com/t/activerecord-problems-from-within-template/94963/1 "2006-05-18T07:04:03Z")

</div>

Hi,  
my Film model has this:

has\_many :live\_action\_personnel,  
:class\_name =\> “Credit”,  
:include =\> :credit\_type,  
:order =\> ‘credit\_types.position,  
credits.position ASC’,  
:group =\> ‘credit\_types.personnel\_type’,  
:conditions =\>  
‘credit\_types.personnel\_type=“live\_action\_personnel”’

If I try and test this for emptiness:  
if @film.live\_action\_personnel.size \> 0  
I get the following error:

MysqlError: Unknown table ‘credit\_types’ in where clause: SELECT  
count(\*) AS count\_all FROM credits WHERE (credits.film\_id = 16 AND  
(credit\_types.personnel\_type=“live\_action\_personnel”))

but if I debug(@film.live\_action\_personnel) earlier within the same  
template, all is well!  
It seems that ActiveRecord isn’t generating the right query when I only  
need to count the number of records.

Anyone know a way round this that doesn’t involve writing my own sql?

thanks

dorian

---

<div class="post-metadata">

**Author:** ![tachekent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7993a0/32.png) [@tachekent](https://www.ruby-forum.com/u/tachekent)\
**Post date:** [May 18, 2006, 4:02pm UTC](https://www.ruby-forum.com/t/activerecord-problems-from-within-template/94963/2 "2006-05-18T16:02:22Z")

</div>

hmm…

it appears that if I use  
@film.live\_action\_personnel(true).size  
then all is ok.

My understanding is that this omits object caching of the sql, but I  
can’t see why it would change the actual sql produced.  
Might this be a bug?

dorian
