Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make sure join association methods are called once.

  • Loading branch information...
commit 54fcbb881d1d707d65d38cd30f50049023448832 1 parent b324740
@miloops miloops authored
View
19 activerecord/lib/active_record/associations.rb
@@ -2059,6 +2059,7 @@ def initialize(reflection, join_dependency, parent = nil)
@aliased_prefix = "t#{ join_dependency.joins.size }"
@parent_table_name = parent.active_record.table_name
@aliased_table_name = aliased_table_name_for(table_name)
+ @join = nil
if reflection.macro == :has_and_belongs_to_many
@aliased_join_table_name = aliased_table_name_for(reflection.options[:join_table], "_join")
@@ -2070,9 +2071,9 @@ def initialize(reflection, join_dependency, parent = nil)
end
def association_join
+ return @join if @join
connection = reflection.active_record.connection
-
- join = case reflection.macro
+ @join = case reflection.macro
when :has_and_belongs_to_many
["%s.%s = %s.%s " % [
connection.quote_table_name(aliased_join_table_name),
@@ -2182,14 +2183,14 @@ def association_join
else
""
end
- join << %(AND %s) % [
+ @join << %(AND %s) % [
klass.send(:type_condition, aliased_table_name)] unless klass.descends_from_active_record?
[through_reflection, reflection].each do |ref|
- join << "AND #{interpolate_sql(sanitize_sql(ref.options[:conditions], aliased_table_name))} " if ref && ref.options[:conditions]
+ @join << "AND #{interpolate_sql(sanitize_sql(ref.options[:conditions], aliased_table_name))} " if ref && ref.options[:conditions]
end
- join
+ @join
end
def relation
@@ -2203,12 +2204,12 @@ def relation
end
def join_relation(joining_relation, join = nil)
- if relation.is_a?(Array)
+ if (relations = relation).is_a?(Array)
joining_relation.
- joins(relation.first, Arel::OuterJoin).on(association_join.first).
- joins(relation.last, Arel::OuterJoin).on(association_join.last)
+ joins(relations.first, Arel::OuterJoin).on(association_join.first).
+ joins(relations.last, Arel::OuterJoin).on(association_join.last)
else
- joining_relation.joins(relation, Arel::OuterJoin).on(association_join)
+ joining_relation.joins(relations, Arel::OuterJoin).on(association_join)
end
end
View
8 activerecord/lib/active_record/base.rb
@@ -1815,11 +1815,11 @@ def build_association_joins(joins)
join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(self, joins, nil)
relation = arel_table.relation
join_dependency.join_associations.map { |association|
- if association.relation.is_a?(Array)
- [Arel::InnerJoin.new(relation, association.relation.first, association.association_join.first).joins(relation),
- Arel::InnerJoin.new(relation, association.relation.last, association.association_join.last).joins(relation)].join()
+ if (association_relation = association.relation).is_a?(Array)
+ [Arel::InnerJoin.new(relation, association_relation.first, association.association_join.first).joins(relation),
+ Arel::InnerJoin.new(relation, association_relation.last, association.association_join.last).joins(relation)].join()
else
- Arel::InnerJoin.new(relation, association.relation, association.association_join).joins(relation)
+ Arel::InnerJoin.new(relation, association_relation, association.association_join).joins(relation)
end
}.join(" ")
end
Please sign in to comment.
Something went wrong with that request. Please try again.