From a9fb1544b0e7e6412e4cb64702585024e24276bf Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Wed, 5 Apr 2006 17:01:56 +0000 Subject: Added support for eagerly including polymorphic has_one associations. (closes #4525) [Rick] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4170 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/associations.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 5319fa39e8..7b6257ec03 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1392,7 +1392,7 @@ module ActiveRecord @aliased_prefix = "t#{ join_dependency.joins.size }" @aliased_table_name = table_name # start with the table name @parent_table_name = parent.active_record.table_name - + if !parent.table_joins.blank? && parent.table_joins.to_s.downcase =~ %r{join(\s+\w+)?\s+#{aliased_table_name.downcase}\son} join_dependency.table_aliases[aliased_table_name] += 1 end @@ -1473,12 +1473,16 @@ module ActiveRecord aliased_table_name, "#{reflection.options[:as]}_type", klass.quote(parent.active_record.base_class.name) ] - + when reflection.macro == :has_one && reflection.options[:as] + " LEFT OUTER JOIN %s ON %s.%s = %s.%s AND %s.%s = %s " % [ + table_name_and_alias, + aliased_table_name, "#{reflection.options[:as]}_id", + parent.aliased_table_name, parent.primary_key, + aliased_table_name, "#{reflection.options[:as]}_type", + klass.quote(reflection.active_record.name) + ] else - foreign_key = options[:foreign_key] || case reflection.macro - when :has_many then reflection.active_record.to_s.classify - when :has_one then reflection.active_record.to_s - end.foreign_key + foreign_key = options[:foreign_key] || reflection.active_record.name.foreign_key " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [ table_name_and_alias, aliased_table_name, foreign_key, -- cgit v1.2.3