Possible bug: belongs_to with :class_name and eager loading

In my model:
class Call < ActiveRecord::Base
belongs_to :doctor, :class_name => ‘User’
end

In the migration:
t.column :doctor_id, :int

call.doctor.last_name works great. But my view had an N+1 problem.
So I modified my find to this:

@calls = Call.find(:all, :conditions => { :day =>
(d-1.day)…d.end_of_month }, :include => [:doctor])

Suddenly my code blew up with an SQL error. The SQL contained
calls.user_id instead of calls.doctor_id. I found the fragment of
code causing the error. In Rails 2.0.2, this is lines 1704-1711 of
active_record/associations.rb:

          when :belongs_to
              " #{join_type} %s ON %s.%s = %s.%s " % [
                 table_name_and_alias,
     connection.quote_table_name(aliased_table_name),
     reflection.klass.primary_key,

connection.quote_table_name(parent.aliased_table_name),
options[:foreign_key] || klass.to_s.foreign_key
]

I worked around this possible bug by changing my model code to:

belongs_to :doctor, :class_name => ‘User’, :foreign_key =>
‘doctor_id’

It seems I shouldn’t have to do that. Is this a bug?

thanks,
Bryan

On 4 Jan 2008, at 08:24, Bryan L. wrote:

So I modified my find to this:

@calls = Call.find(:all, :conditions => { :day =>
(d-1.day)…d.end_of_month }, :include => [:doctor])

Suddenly my code blew up with an SQL error. The SQL contained
calls.user_id instead of calls.doctor_id. I found the fragment of
code causing the error. In Rails 2.0.2, this is lines 1704-1711 of
active_record/associations.rb:

THis appears relevant: http://dev.rubyonrails.org/changeset/8456
Looks like this was a bug and it has been fixed
Fred