Forum: Ruby on Rails bug rails activerecord association join

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Onur T. (Guest)
on 2006-01-06 10:28
(Received via mailing list)
I found a bug in ActiveRecord association SQL INNER JOIN codes. in
has_many, has_one, habtm and belongs_to queries the foreign key is
determined from the table name of reflection and self. but that's
worng because if you use a global prefix or suffix for your tables the
foreign keys will be in format of PREFIX_CLASSNAME_SUFFIX_id which is
wrong, because it breaks the advantage of changing the prefix and
giving it a go without chanigng any field names.. on the other hand
the associations  lik @blog.comments work just normal (using
CLASSNAME_id).  I fixed the problem by changing the determination of
foreign key from table name to class name as following.

I removed table_name references and replaced them with to_s's to get
class names.. I thought of using class_name method but it gives the
parent class's name in STI (I dont know if its the desired behaviour
or is it also a bug)

Is there a better way of doing this? I wanna submit a patch but
couldnt be sure if I did it correct.

module ActiveRecord
  module Associations
    def self.append_features(base)

    module ClassMethods
    def association_join(reflection)
          case reflection.macro
            when :has_and_belongs_to_many
              " LEFT OUTER JOIN #{reflection.options[:join_table]} ON "
|| to_s.foreign_key} = " +
              "#{table_name}.#{primary_key} " +
              " LEFT OUTER JOIN #{reflection.klass.table_name} ON " +
|| reflection.klass.to_s.foreign_key} = " +
            when :has_many, :has_one
              " LEFT OUTER JOIN #{reflection.klass.table_name} ON " +
|| to_s.foreign_key} = " +
              "#{table_name}.#{primary_key} "
            when :belongs_to
              " LEFT OUTER JOIN #{reflection.klass.table_name} ON " +
= " +
              "#{table_name}.#{reflection.options[:foreign_key] ||
reflection.klass.to_s.foreign_key} "
This topic is locked and can not be replied to.