Skip to content

Commit

Permalink
explicitly allowing lolqueries
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Feb 16, 2011
1 parent ceb2f0f commit 9c023cc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
11 changes: 1 addition & 10 deletions activerecord/lib/active_record/relation/query_methods.rb
Expand Up @@ -209,16 +209,7 @@ def custom_join_ast(table, joins)
def collapse_wheres(arel, wheres)
equalities = wheres.grep(Arel::Nodes::Equality)

groups = equalities.group_by do |equality|
equality.left
end

groups.each do |_, eqls|
test = eqls.inject(eqls.shift) do |memo, expr|
memo.or(expr)
end
arel.where(test)
end
arel.where(Arel::Nodes::And.new(equalities)) unless equalities.empty?

(wheres - equalities).each do |where|
where = Arel.sql(where) if String === where
Expand Down
13 changes: 10 additions & 3 deletions activerecord/test/cases/relations_test.rb
Expand Up @@ -474,10 +474,17 @@ def test_find_all_using_where_twice_should_or_the_relation
relation = relation.where(:name => david.name)
relation = relation.where(:name => 'Santiago')
relation = relation.where(:id => david.id)
assert_equal [david], relation.all
assert_equal [], relation.all
end

def test_find_all_with_multiple_ors
def test_multi_where_ands_queries
relation = Author.unscoped
david = authors(:david)
sql = relation.where(:name => david.name).where(:name => 'Santiago').to_sql
assert_match('AND', sql)
end

def test_find_all_with_multiple_should_use_and
david = authors(:david)
relation = [
{ :name => david.name },
Expand All @@ -486,7 +493,7 @@ def test_find_all_with_multiple_ors
].inject(Author.unscoped) do |memo, param|
memo.where(param)
end
assert_equal [david], relation.all
assert_equal [], relation.all
end

def test_find_all_using_where_with_relation
Expand Down

4 comments on commit 9c023cc

@ernie
Copy link
Contributor

@ernie ernie commented on 9c023cc Feb 16, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah, nice commit message :)

@exviva
Copy link
Contributor

@exviva exviva commented on 9c023cc Feb 17, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's a lolquery? :)

@stevehill1981
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like to me that it's a query that can never return any results, because of the conditions specified.

@ernie
Copy link
Contributor

@ernie ernie commented on 9c023cc Feb 17, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, see the recent discussion on the rails core list for the story.

Please sign in to comment.