aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/relation')
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb45
1 files changed, 19 insertions, 26 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index 6886c7538b..08b61c9752 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -163,25 +163,25 @@ module ActiveRecord
end
def build_arel
- arel = table
+ arel = table.from table
- arel = build_joins(arel, @joins_values) unless @joins_values.empty?
+ build_joins(arel, @joins_values) unless @joins_values.empty?
- arel = collapse_wheres(arel, (@where_values - ['']).uniq)
+ collapse_wheres(arel, (@where_values - ['']).uniq)
- arel = arel.having(*@having_values.uniq.reject{|h| h.blank?}) unless @having_values.empty?
+ arel.having(*@having_values.uniq.reject{|h| h.blank?}) unless @having_values.empty?
- arel = arel.take(@limit_value) if @limit_value
- arel = arel.skip(@offset_value) if @offset_value
+ arel.take(@limit_value) if @limit_value
+ arel.skip(@offset_value) if @offset_value
- arel = arel.group(*@group_values.uniq.reject{|g| g.blank?}) unless @group_values.empty?
+ arel.group(*@group_values.uniq.reject{|g| g.blank?}) unless @group_values.empty?
- arel = arel.order(*@order_values.uniq.reject{|o| o.blank?}) unless @order_values.empty?
+ arel.order(*@order_values.uniq.reject{|o| o.blank?}) unless @order_values.empty?
- arel = build_select(arel, @select_values.uniq)
+ build_select(arel, @select_values.uniq)
- arel = arel.from(@from_value) if @from_value
- arel = arel.lock(@lock_value) if @lock_value
+ arel.from(@from_value) if @from_value
+ arel.lock(@lock_value) if @lock_value
arel
end
@@ -247,7 +247,7 @@ module ActiveRecord
end
end
- def build_joins(relation, joins)
+ def build_joins(manager, joins)
joins = joins.map {|j| j.respond_to?(:strip) ? j.strip : j}.uniq
association_joins = joins.find_all do |join|
@@ -257,7 +257,7 @@ module ActiveRecord
stashed_association_joins = joins.grep(ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation)
non_association_joins = (joins - association_joins - stashed_association_joins)
- join_ast = custom_join_ast(relation, non_association_joins)
+ join_ast = custom_join_ast(manager.froms.first, non_association_joins)
join_dependency = ActiveRecord::Associations::ClassMethods::JoinDependency.new(@klass, association_joins, join_ast)
@@ -267,21 +267,14 @@ module ActiveRecord
# FIXME: refactor this to build an AST
join_dependency.join_associations.each do |association|
- relation = association.join_to(relation)
+ manager = association.join_to(manager)
end
- if Arel::Table === relation
- relation.from(join_ast || relation)
- else
- if relation.froms.length > 0 && join_ast
- join_ast.left = relation.froms.first
- relation.from join_ast
- elsif relation.froms.length == 0 && join_ast
- relation.from(join_ast)
- else
- relation
- end
- end
+ return manager unless join_ast
+
+ join_ast.left = manager.froms.first
+ manager.from join_ast
+ manager
end
def build_select(arel, selects)