diff options
author | Rick Olson <technoweenie@gmail.com> | 2006-04-05 17:11:21 +0000 |
---|---|---|
committer | Rick Olson <technoweenie@gmail.com> | 2006-04-05 17:11:21 +0000 |
commit | 58c435a3caebbe31c02e80c8f32840f0daa57d68 (patch) | |
tree | ffecd5a19ee67a1bc70223141955006a3f8bac61 /activerecord/lib/active_record | |
parent | a9fb1544b0e7e6412e4cb64702585024e24276bf (diff) | |
download | rails-58c435a3caebbe31c02e80c8f32840f0daa57d68.tar.gz rails-58c435a3caebbe31c02e80c8f32840f0daa57d68.tar.bz2 rails-58c435a3caebbe31c02e80c8f32840f0daa57d68.zip |
Support eager includes when going through a polymorphic has_many association. [Rick]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4171 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record')
-rwxr-xr-x | activerecord/lib/active_record/associations.rb | 50 |
1 files changed, 32 insertions, 18 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 7b6257ec03..8b6aac49ce 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1445,24 +1445,38 @@ module ActiveRecord aliased_table_name, primary_key, aliased_join_table_name, options[:foreign_key] || reflection.klass.to_s.classify.foreign_key ] else - case source_reflection.macro - when :belongs_to - first_key = primary_key - second_key = options[:foreign_key] || klass.to_s.classify.foreign_key - when :has_many - first_key = through_reflection.klass.to_s.classify.foreign_key - second_key = options[:foreign_key] || primary_key + if source_reflection.macro == :has_many && source_reflection.options[:as] + " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [ + table_alias_for(through_reflection.klass.table_name, aliased_join_table_name), aliased_join_table_name, + through_reflection.primary_key_name, + parent.aliased_table_name, parent.primary_key] + + " LEFT OUTER JOIN %s ON %s.%s = %s.%s AND %s.%s = %s " % [ + table_name_and_alias, + aliased_table_name, "#{source_reflection.options[:as]}_id", + aliased_join_table_name, options[:foreign_key] || primary_key, + aliased_table_name, "#{source_reflection.options[:as]}_type", + klass.quote(source_reflection.active_record.base_class.name) + ] + else + case source_reflection.macro + when :belongs_to + first_key = primary_key + second_key = options[:foreign_key] || klass.to_s.classify.foreign_key + when :has_many + first_key = through_reflection.klass.to_s.classify.foreign_key + second_key = options[:foreign_key] || primary_key + end + + " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [ + table_alias_for(through_reflection.klass.table_name, aliased_join_table_name), aliased_join_table_name, + through_reflection.primary_key_name, + parent.aliased_table_name, parent.primary_key] + + " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [ + table_name_and_alias, + aliased_table_name, first_key, + aliased_join_table_name, second_key + ] end - - " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [ - table_alias_for(through_reflection.klass.table_name, aliased_join_table_name), aliased_join_table_name, - through_reflection.primary_key_name, - parent.aliased_table_name, parent.primary_key] + - " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [ - table_name_and_alias, - aliased_table_name, first_key, - aliased_join_table_name, second_key - ] end when reflection.macro == :has_many && reflection.options[:as] @@ -1479,7 +1493,7 @@ module ActiveRecord 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) + klass.quote(reflection.active_record.base_class.name) ] else foreign_key = options[:foreign_key] || reflection.active_record.name.foreign_key |