Skip to content

Commit

Permalink
only returning where values for the corresponding relation, also filt…
Browse files Browse the repository at this point in the history
…ering where value hash based on table name [#5234 state:resolved] [#5184 state:resolved]
  • Loading branch information
tenderlove committed Oct 30, 2010
1 parent cbca12f commit 296467f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
9 changes: 7 additions & 2 deletions activerecord/lib/active_record/relation.rb
Expand Up @@ -319,8 +319,13 @@ def to_sql
end

def where_values_hash
Hash[@where_values.find_all {|w| w.respond_to?(:operator) && w.operator == :== }.map {|where|
[where.operand1.name, where.operand2.respond_to?(:value) ? where.operand2.value : where.operand2]
Hash[@where_values.find_all { |w|
w.respond_to?(:operator) && w.operator == :== && w.left.relation.name == table_name
}.map { |where|
[
where.left.name,
where.right.respond_to?(:value) ? where.right.value : where.right
]
}]
end

Expand Down
9 changes: 5 additions & 4 deletions activerecord/lib/active_record/relation/spawn_methods.rb
Expand Up @@ -30,13 +30,14 @@ def merge(r)

unless @where_values.empty?
# Remove duplicates, last one wins.
seen = {}
seen = Hash.new { |h,table| h[table] = {} }
merged_wheres = merged_wheres.reverse.reject { |w|
nuke = false
if w.respond_to?(:operator) && w.operator == :==
name = w.left.name
nuke = seen[name]
seen[name] = true
name = w.left.name
table = w.left.relation.name
nuke = seen[table][name]
seen[table][name] = true
end
nuke
}.reverse
Expand Down

0 comments on commit 296467f

Please sign in to comment.