Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Passing nil in array generates improper SQL #40

Merged
merged 1 commit into from

2 participants

@gmile

Currently Arel generate a pretty odd queue when working with an array containing nil value. This commit fixes the problem (which exists, from my perspective).

When calling:

where(:field => [1, 2, 3, nil])

the following SQL is generated:

field IN (1, 2, 3, NULL)

while it should be more sqlish and look like

field IN (1, 2, 3) OR field IS NULL

This issue is affiliated with this pull (rails repo) request, which brought tests.

@gmile gmile Generate more sqlish queue.
Now, instead of the following SQL code:
  some_field IN (1, 2, NULL)

Arel will generate the proper one:
  some_field IN (1, 2) OR IS NULL
fe5719f
@tenderlove
Owner

I'm merging this in, but please add tests to ARel next time. I don't want to run the AR tests every time to ensure no regressions happen in ARel. :-(

@tenderlove tenderlove referenced this pull request from a commit
@tenderlove tenderlove Merged pull request #40 from gmile/master.
Passing nil in array generates improper SQL
490d6f9
@tenderlove tenderlove merged commit 490d6f9 into rails:master
@tenderlove tenderlove referenced this pull request from a commit
@tenderlove tenderlove Revert "Merged pull request #40 from gmile/master."
This reverts commit 490d6f9, reversing
changes made to dd07005.
d09a882
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 24, 2011
  1. @gmile

    Generate more sqlish queue.

    gmile authored
    Now, instead of the following SQL code:
      some_field IN (1, 2, NULL)
    
    Arel will generate the proper one:
      some_field IN (1, 2) OR IS NULL
This page is out of date. Refresh to see the latest.
Showing with 11 additions and 1 deletion.
  1. +11 −1 lib/arel/predications.rb
View
12 lib/arel/predications.rb
@@ -41,7 +41,17 @@ def in other
Nodes::Between.new(self, Nodes::And.new([other.begin, other.end]))
end
else
- Nodes::In.new self, other
+ if other.include?(nil)
+ if other.size > 1
+ set = Nodes::In.new self, other.compact
+ null = Nodes::Equality.new self, nil
+ Nodes::Or.new set, null
+ else
+ Nodes::Equality.new self, nil
+ end
+ else
+ Nodes::In.new self, other
+ end
end
end
Something went wrong with that request. Please try again.