Forum: Ruby on Rails ActiveRecord problems from within template

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Dorian M. (Guest)
on 2006-05-18 09:04
(Received via mailing list)
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
Dorian M. (Guest)
on 2006-05-18 18:02
(Received via mailing list)
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
This topic is locked and can not be replied to.