Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AquaGeek/971744 to your computer and use it in GitHub Desktop.
Save AquaGeek/971744 to your computer and use it in GitHub Desktop.
Rails Lighthouse ticket #6133
From c2007f71b13526f0c6bdfe44e358973ce0120daf Mon Sep 17 00:00:00 2001
From: gmile <iamexile@gmail.com>
Date: Sat, 12 Feb 2011 14:27:46 +0200
Subject: [PATCH] Generate 'IS NULL' instead of IN (NULL)
---
lib/arel/predications.rb | 12 +++++++++++-
1 files changed, 11 insertions(+), 1 deletions(-)
diff --git a/lib/arel/predications.rb b/lib/arel/predications.rb
index 58f02a2..49741df 100644
--- a/lib/arel/predications.rb
+++ b/lib/arel/predications.rb
@@ -41,7 +41,17 @@ module Arel
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
--
1.7.1
From d9fefa4c5b56deb7fa35c1859285c6cba6fa3244 Mon Sep 17 00:00:00 2001
From: gmile <iamexile@gmail.com>
Date: Sat, 12 Feb 2011 14:42:20 +0200
Subject: [PATCH] Test that passing nil member of array in conditions retrieves records with nil
value on a selected field.
---
activerecord/test/cases/finder_test.rb | 22 ++++++++++++++++++++++
1 files changed, 22 insertions(+), 0 deletions(-)
diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb
index c35590b..c53f7fe 100644
--- a/activerecord/test/cases/finder_test.rb
+++ b/activerecord/test/cases/finder_test.rb
@@ -997,6 +997,28 @@ class FinderTest < ActiveRecord::TestCase
:order => ' author_addresses_authors.id DESC ', :limit => 3).size
end
+ def test_find_with_nil_inside_set_passed_for_attribute
+ client_of = Company.find(
+ :all,
+ :conditions => {
+ :client_of => [2, 1, nil],
+ :name => ['37signals', 'Summit', 'Microsoft'] },
+ :order => 'client_of DESC'
+ ).map { |x| x.client_of }
+
+ assert_equal [2, 1, nil], client_of
+ end
+
+ def test_find_with_single_nil_inside_set_passed_for_attribute
+ client_of = Company.find(
+ :all,
+ :conditions => { :client_of => [nil] },
+ :order => 'client_of DESC'
+ ).map { |x| x.client_of }
+
+ assert_equal [nil], client_of
+ end
+
def test_with_limiting_with_custom_select
posts = Post.find(:all, :include => :author, :select => ' posts.*, authors.id as "author_id"', :limit => 3, :order => 'posts.id')
assert_equal 3, posts.size
--
1.7.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment