# Nil in ActiveRecord and SQL

**URL:** <https://www.ruby-forum.com/t/nil-in-activerecord-and-sql/83342>\
**Category:** Rails\
**Created:** [January 25, 2007, 12:50pm UTC](https://www.ruby-forum.com/t/nil-in-activerecord-and-sql/83342 "2007-01-25T12:50:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alabama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/858c86/32.png) [@alabama](https://www.ruby-forum.com/u/alabama)\
**Post date:** [January 25, 2007, 12:50pm UTC](https://www.ruby-forum.com/t/nil-in-activerecord-and-sql/83342/1 "2007-01-25T12:50:27Z")

</div>

Hi all !  
How I can do like this, because it dosen’t work ?  
SomethingClass.find(:all, :conditions =\> [“something\_attribute =  
?”,nil])

---

<div class="post-metadata">

**Author:** ![alabama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/858c86/32.png) [@alabama](https://www.ruby-forum.com/u/alabama)\
**Post date:** [January 25, 2007, 1:11pm UTC](https://www.ruby-forum.com/t/nil-in-activerecord-and-sql/83342/2 "2007-01-25T13:11:39Z")

</div>

On 1/25/07, Walde M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How I can do like this, because it dosen’t work ?  
> SomethingClass.find(:all, :conditions =\> [“something\_attribute =  
> ?”,nil])

In SQL, you need “something IS NULL” rather than “something = NULL”.  
The above code should have raised a StatementInvalid exception saying  
as much.

Pass conditions as a hash to have Active Record choose whether to use  
IS NULL for you:  
SomethingClass.find(:all, :conditions =\> { :something\_attribute =\> nil  
})

Or use the find\_all\_by\_\* shorthand:  
SomethingClass.find\_all\_by\_something\_attribute(nil)

jeremy

---

<div class="post-metadata">

**Author:** ![alabama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/858c86/32.png) [@alabama](https://www.ruby-forum.com/u/alabama)\
**Post date:** [January 25, 2007, 2:01pm UTC](https://www.ruby-forum.com/t/nil-in-activerecord-and-sql/83342/3 "2007-01-25T14:01:36Z")

</div>

Jeremy K. wrote:

> On 1/25/07, Walde M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > How I can do like this, because it dosen’t work ?  
> > SomethingClass.find(:all, :conditions =\> [“something\_attribute =  
> > ?”,nil])
> 
> In SQL, you need “something IS NULL” rather than “something = NULL”.  
> The above code should have raised a StatementInvalid exception saying  
> as much.
> 
> Pass conditions as a hash to have Active Record choose whether to use  
> IS NULL for you:  
> SomethingClass.find(:all, :conditions =\> { :something\_attribute =\> nil  
> })
> 
> Or use the find\_all\_by\_\* shorthand:  
> SomethingClass.find\_all\_by\_something\_attribute(nil)
> 
> jeremy

Thank for help Jeremy !  
SomethingClass.find(:all, :conditions =\> { :something\_attribute =\> nil

> }) It work excellently !!!

But of course, problem is bigger.  
When I try do something like that :

SomethingClass.find(:all, :conditions =\> ({ :something\_attribute1 =\> nil

> } or { :something\_attribute1 =\> Time.now.mday } ) and ({ :something\_attribute2 =\> nil  
> } or { :something\_attribute2 =\> Time.now.hour } ) and ({ :something\_attribute3 =\> nil  
> } or { :something\_attribute3 =\> Time.now.min } ))  
> it doesn’t work again.  
> Could you explain me how I should use “or” and “and” .

> })

---

<div class="post-metadata">

**Author:** ![alabama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/858c86/32.png) [@alabama](https://www.ruby-forum.com/u/alabama)\
**Post date:** [January 25, 2007, 2:51pm UTC](https://www.ruby-forum.com/t/nil-in-activerecord-and-sql/83342/4 "2007-01-25T14:51:35Z")

</div>

Jeremy K. wrote:

> On 1/25/07, Walde M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> Check out the ez\_where plugin if you’d like to easily translate Ruby’s  
> operators to SQL.
> 
> jeremy

One more ,thank for help Jeremy !  
On another way I apply “IS NULL” in “text form of conditions” instead  
of “= nil”.  
And with “or” and “and”, is great !  
… a thousand thanks !

---

<div class="post-metadata">

**Author:** ![alabama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/858c86/32.png) [@alabama](https://www.ruby-forum.com/u/alabama)\
**Post date:** [January 25, 2007, 2:06pm UTC](https://www.ruby-forum.com/t/nil-in-activerecord-and-sql/83342/5 "2007-01-25T14:06:51Z")

</div>

On 1/25/07, Walde M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> Thank for help Jeremy !  
> it doesn’t work again.  
> Could you explain me how I should use “or” and “and” .

Check out the ez\_where plugin if you’d like to easily translate Ruby’s  
operators to SQL.

jeremy
