diff options
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG | 6 | ||||
-rwxr-xr-x | activerecord/lib/active_record/associations.rb | 34 | ||||
-rwxr-xr-x | activerecord/test/associations_test.rb | 4 |
3 files changed, 32 insertions, 12 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 8dd5a86360..e0803611cb 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,11 @@ *SVN* +* Alias the has_and_belongs_to_many join table on eager includes. #4106 [jeremyevans0@gmail.com] + + This statement would normally error because the projects_developers table is joined twice, and therefore joined_on would be ambiguous. + + Developer.find(:all, :include => {:projects => :developers}, :conditions => 'join_project_developers.joined_on IS NOT NULL') + * Oracle adapter gets some love #4230 [schoenm@earthlink.net] * Changes :text to CLOB rather than BLOB [Moses Hohman] diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 0f25f96cee..072e35aadd 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1200,7 +1200,7 @@ module ActiveRecord end class JoinAssociation < JoinBase - attr_reader :reflection, :parent, :aliased_table_name, :aliased_prefix + attr_reader :reflection, :parent, :aliased_table_name, :aliased_prefix, :aliased_join_table_name delegate :options, :klass, :to=>:reflection def initialize(reflection, join_dependency, parent = nil) @@ -1214,6 +1214,13 @@ module ActiveRecord # if the alias has been used, add a '_n' suffix to the end. @aliased_table_name = "#{parent.active_record.to_s.underscore}_#{reflection.name}_#{join_dependency.table_aliases[aliased_table_name]}".gsub(/_1$/, '') end + if reflection.macro == :has_and_belongs_to_many || (reflection.macro == :has_many && reflection.options[:through]) + @aliased_join_table_name = reflection.macro == :has_and_belongs_to_many ? reflection.options[:join_table] : parent.active_record.reflect_on_association(reflection.options[:through]).klass.table_name + unless join_dependency.table_aliases[aliased_join_table_name].zero? + @aliased_join_table_name = "join_#{parent.active_record.to_s.underscore}_#{reflection.name}_#{join_dependency.table_aliases[aliased_join_table_name]}".gsub(/_1$/, '') + end + join_dependency.table_aliases[aliased_join_table_name] += 1 + end join_dependency.table_aliases[aliased_table_name] += 1 end @@ -1221,8 +1228,8 @@ module ActiveRecord join = case reflection.macro when :has_and_belongs_to_many join_table_name = - " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [ - options[:join_table], options[:join_table], + " LEFT OUTER JOIN %s %s ON %s.%s = %s.%s " % [ + options[:join_table], aliased_join_table_name, aliased_join_table_name, options[:foreign_key] || reflection.active_record.to_s.classify.foreign_key, reflection.active_record.table_name, reflection.active_record.primary_key] + " LEFT OUTER JOIN %s %s ON %s.%s = %s.%s " % [ @@ -1233,24 +1240,27 @@ module ActiveRecord case when reflection.macro == :has_many && reflection.options[:through] through_reflection = parent.active_record.reflect_on_association(reflection.options[:through]) - aliased_through_table = "#{parent.table_name}_#{through_reflection.klass.table_name}" if through_reflection.options[:as] # has_many :through against a polymorphic join polymorphic_foreign_key = through_reflection.options[:as].to_s + '_id' polymorphic_foreign_type = through_reflection.options[:as].to_s + '_type' - " LEFT OUTER JOIN %s %s ON (%s.%s = %s.%s AND %s.%s = %s) " % [through_reflection.klass.table_name, aliased_through_table, - aliased_through_table, polymorphic_foreign_key, + " LEFT OUTER JOIN %s %s ON (%s.%s = %s.%s AND %s.%s = %s) " % [ + through_reflection.klass.table_name, aliased_join_table_name, + aliased_join_table_name, polymorphic_foreign_key, parent.aliased_table_name, parent.primary_key, - aliased_through_table, polymorphic_foreign_type, klass.quote(parent.active_record.base_class.name)] + + aliased_join_table_name, polymorphic_foreign_type, klass.quote(parent.active_record.base_class.name)] + " LEFT OUTER JOIN %s %s ON %s.%s = %s.%s " % [table_name, aliased_table_name, - aliased_table_name, primary_key, aliased_through_table, options[:foreign_key] || reflection.klass.to_s.classify.foreign_key + aliased_table_name, primary_key, aliased_join_table_name, options[:foreign_key] || reflection.klass.to_s.classify.foreign_key ] else # has_many :through against a normal join - " LEFT OUTER JOIN %s %s ON %s.%s = %s.%s " % [through_reflection.klass.table_name, aliased_through_table, - aliased_through_table, through_reflection.options[:foreign_key] || through_reflection.active_record.to_s.classify.foreign_key, + " LEFT OUTER JOIN %s %s ON %s.%s = %s.%s " % [ + through_reflection.klass.table_name, aliased_join_table_name, aliased_join_table_name, + through_reflection.options[:foreign_key] || through_reflection.active_record.to_s.classify.foreign_key, parent.aliased_table_name, parent.primary_key] + - " LEFT OUTER JOIN %s %s ON %s.%s = %s.%s " % [table_name, aliased_table_name, - aliased_table_name, primary_key, aliased_through_table, options[:foreign_key] || klass.to_s.classify.foreign_key + " LEFT OUTER JOIN %s %s ON %s.%s = %s.%s " % [ + table_name, aliased_table_name, + aliased_table_name, primary_key, + aliased_join_table_name, options[:foreign_key] || klass.to_s.classify.foreign_key ] end diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index c4c497575b..89d85bd567 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -1469,4 +1469,8 @@ class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase AND developer_id = #{developer.id} end_sql end + + def test_join_table_alias + assert_equal 3, Developer.find(:all, :include => {:projects => :developers}, :conditions => 'join_project_developers.joined_on IS NOT NULL').size + end end |