aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/merger.rb
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2012-04-22 11:34:55 +0200
committerJon Leighton <j@jonathanleighton.com>2012-04-25 15:45:06 +0100
commitef1d1e1492e3c48884f7653103bc9313dd04cfad (patch)
tree63ec275d561d8e9f6a2f5b44e18b5c676591bbbe /activerecord/lib/active_record/relation/merger.rb
parente8cdb3d5e73c1be3d551f55602de7f5c34d43c42 (diff)
downloadrails-ef1d1e1492e3c48884f7653103bc9313dd04cfad.tar.gz
rails-ef1d1e1492e3c48884f7653103bc9313dd04cfad.tar.bz2
rails-ef1d1e1492e3c48884f7653103bc9313dd04cfad.zip
fix interpolation for hash merging
Diffstat (limited to 'activerecord/lib/active_record/relation/merger.rb')
-rw-r--r--activerecord/lib/active_record/relation/merger.rb39
1 files changed, 24 insertions, 15 deletions
diff --git a/activerecord/lib/active_record/relation/merger.rb b/activerecord/lib/active_record/relation/merger.rb
index 52cf0b7077..3f880ce5e9 100644
--- a/activerecord/lib/active_record/relation/merger.rb
+++ b/activerecord/lib/active_record/relation/merger.rb
@@ -3,32 +3,41 @@ require 'active_support/core_ext/hash/keys'
module ActiveRecord
class Relation
- class Merger
- attr_reader :relation, :other
+ class HashMerger
+ attr_reader :relation, :hash
- def initialize(relation, other)
- @relation = relation
+ def initialize(relation, hash)
+ hash.assert_valid_keys(*Relation::VALUE_METHODS)
- if other.default_scoped? && other.klass != relation.klass
- @other = other.with_default_scope
- else
- @other = other
- end
+ @relation = relation
+ @hash = hash
end
def merge
- HashMerger.new(relation, other.values).merge
+ Merger.new(relation, other).merge
+ end
+
+ # Applying values to a relation has some side effects. E.g.
+ # interpolation might take place for where values. So we should
+ # build a relation to merge in rather than directly merging
+ # the values.
+ def other
+ other = Relation.new(relation.klass, relation.table)
+ hash.each { |k, v| other.send("#{k}!", v) }
+ other
end
end
- class HashMerger
+ class Merger
attr_reader :relation, :values
- def initialize(relation, values)
- values.assert_valid_keys(*Relation::VALUE_METHODS)
+ def initialize(relation, other)
+ if other.default_scoped? && other.klass != relation.klass
+ other = other.with_default_scope
+ end
@relation = relation
- @values = values
+ @values = other.values
end
def normal_values
@@ -85,7 +94,7 @@ module ActiveRecord
def merged_wheres
if values[:where]
- merged_wheres = relation.where_values + Array(values[:where])
+ merged_wheres = relation.where_values + values[:where]
unless relation.where_values.empty?
# Remove duplicates, last one wins.