Permalink
Browse files

Clean up relation joins when finding records with included associations.

  • Loading branch information...
1 parent 4b339f4 commit c923409630a92d1a935699c1427702c822601165 @miloops miloops committed Aug 17, 2009
Showing with 12 additions and 12 deletions.
  1. +12 −12 activerecord/lib/active_record/associations.rb
@@ -1679,12 +1679,7 @@ def construct_finder_arel_with_included_associations(options, join_dependency)
relation = arel_table((scope && scope[:from]) || options[:from])
for association in join_dependency.join_associations
- if association.relation.is_a?(Array)
- relation.join(association.relation.first, association.join_type).on(association.association_join.first)
- relation.join(association.relation.last, association.join_type).on(association.association_join.last)
- else
- relation.join(association.relation, association.join_type).on(association.association_join)
- end
+ relation = association.join_relation(relation)
end
relation.join(construct_join(options[:joins], scope))
@@ -1727,12 +1722,7 @@ def construct_finder_sql_for_association_limiting(options, join_dependency)
relation = arel_table(options[:from])
for association in join_dependency.join_associations
- if association.relation.is_a?(Array)
- relation.join(association.relation.first, association.join_type).on(association.association_join.first)
- relation.join(association.relation.last, association.join_type).on(association.association_join.last)
- else
- relation.join(association.relation, association.join_type).on(association.association_join)
- end
+ relation = association.join_relation(relation)
end
relation.join(construct_join(options[:joins], scope))
@@ -2230,6 +2220,16 @@ def join_type
Arel::OuterJoin
end
+ def join_relation(joining_relation, join = nil)
+ if relation.is_a?(Array)
+ joining_relation.join(relation.first, join_type).on(association_join.first)
+ joining_relation.join(relation.last, join_type).on(association_join.last)
+ else
+ joining_relation.join(relation, join_type).on(association_join)
+ end
+ joining_relation
+ end
+
protected
def aliased_table_name_for(name, suffix = nil)

0 comments on commit c923409

Please sign in to comment.