Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Alias the has_and_belongs_to_many join table on eager includes. closes

…#4106 [jeremyevans0@gmail.com]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3895 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 02d34440cf0713479ec26332bcfc004f343685ab 1 parent 4e15bf7
risk danger olson technoweenie authored
6 activerecord/CHANGELOG
View
@@ -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]
34 activerecord/lib/active_record/associations.rb
View
@@ -1200,7 +1200,7 @@ def instantiate(row)
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 @@ def initialize(reflection, join_dependency, parent = nil)
# 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 @@ def association_join
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 @@ def association_join
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
4 activerecord/test/associations_test.rb
View
@@ -1469,4 +1469,8 @@ def test_update_attributes_after_push_without_duplicate_join_table_rows
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
Please sign in to comment.
Something went wrong with that request. Please try again.