Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use arel predicates instead of strings wherever possible when merging…

… relations
  • Loading branch information...
commit 32b48bf419eb22514be9a75981b9b0cf51a973a0 1 parent f1acf1c
@lifo lifo authored
Showing with 10 additions and 11 deletions.
  1. +10 −11 activerecord/lib/active_record/relation.rb
View
21 activerecord/lib/active_record/relation.rb
@@ -18,19 +18,18 @@ def initialize(klass, relation)
def merge(r)
raise ArgumentError, "Cannot merge a #{r.klass.name} relation with #{@klass.name} relation" if r.klass != @klass
- merged_relation = spawn(table)
+ merged_relation = spawn(table).eager_load(r.eager_load_associations).preload(r.preload_associations)
+ merged_relation.readonly = r.readonly
- [self, r].each do |r|
+ [self.relation, r.relation].each do |arel|
merged_relation = merged_relation.
- joins(r.relation.joins(r.relation)).
- group(r.send(:group_clauses).join(', ')).
- order(r.send(:order_clauses).join(', ')).
- limit(r.taken).
- offset(r.skipped).
- select(r.send(:select_clauses).join(', ')).
- eager_load(r.eager_load_associations).
- preload(r.preload_associations).
- from(r.send(:sources).present? ? r.send(:from_clauses) : nil)
+ joins(arel.joins(arel)).
+ group(arel.groupings).
+ order(arel.send(:order_clauses).join(', ')).
+ limit(arel.taken).
+ offset(arel.skipped).
+ select(arel.send(:select_clauses)).
+ from(arel.sources)
end
merged_wheres = @relation.wheres

1 comment on commit 32b48bf

@boxofrobots

Nice!

Please sign in to comment.
Something went wrong with that request. Please try again.