Bryan Larsen
2008-Jan-04 08:24 UTC
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Jan-04 11:27 UTC
Re: possible bug: belongs_to with :class_name and eager loading
On 4 Jan 2008, at 08:24, Bryan Larsen wrote:> > 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: >THis appears relevant: http://dev.rubyonrails.org/changeset/8456 Looks like this was a bug and it has been fixed Fred> 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 > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---