# Finding records with some attribute set to nil

**URL:** <https://www.ruby-forum.com/t/finding-records-with-some-attribute-set-to-nil/89141>\
**Category:** Rails\
**Created:** [March 17, 2007, 8:12pm UTC](https://www.ruby-forum.com/t/finding-records-with-some-attribute-set-to-nil/89141 "2007-03-17T20:12:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dxk3355](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@dxk3355](https://www.ruby-forum.com/u/dxk3355)\
**Post date:** [March 17, 2007, 8:12pm UTC](https://www.ruby-forum.com/t/finding-records-with-some-attribute-set-to-nil/89141/1 "2007-03-17T20:12:33Z")

</div>

Hi!

Suppose there is boolean attribute “has\_been\_read” on Posts table… how  
do I find all the records that have this attribute set to NULL ?

This seems logical, but it doesn’t work (I tried other things as well):

Post.find(:all, :conditions =\> ["has\_been\_read = ?, nil])

Thank you!

David

---

<div class="post-metadata">

**Author:** ![dxk3355](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@dxk3355](https://www.ruby-forum.com/u/dxk3355)\
**Post date:** [March 17, 2007, 10:30pm UTC](https://www.ruby-forum.com/t/finding-records-with-some-attribute-set-to-nil/89141/2 "2007-03-17T22:30:26Z")

</div>

D. Krmpotic wrote:

> Post.find(:all, :conditions =\> ["has\_been\_read = ?, nil])

Post.find(:all, :conditions =\> [“has\_been\_read IS NULL”])

That will work.

Unless you’re keeping some other kind of data in that column, though,  
you might prefer use a migration to change it to boolean, default false:

change\_column :\<table\_name\>, :has\_been\_read, :boolean, :default =\> false

This way, you can say

Post.find(:all, :conditions =\> ["has\_been\_read = ?, false])

And in your model, put something like

def mark\_as\_read  
self.update\_attribute(‘has\_been\_read’, true)  
end

Then you just call @thing.mark\_as\_read when it’s read, and you should  
have everything you need.

–Al Evans

---

<div class="post-metadata">

**Author:** ![dxk3355](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@dxk3355](https://www.ruby-forum.com/u/dxk3355)\
**Post date:** [March 17, 2007, 10:59pm UTC](https://www.ruby-forum.com/t/finding-records-with-some-attribute-set-to-nil/89141/3 "2007-03-17T22:59:22Z")

</div>

thank you for responding with great answer.

Thank you for the recommendations as well… I intend to do all that… I  
realized that allowing nulls in boolean column is bad…

thank you again!

david

Al Evans wrote:

> D. Krmpotic wrote:
> 
> > Post.find(:all, :conditions =\> ["has\_been\_read = ?, nil])
> 
> Post.find(:all, :conditions =\> [“has\_been\_read IS NULL”])
> 
> That will work.
> 
> Unless you’re keeping some other kind of data in that column, though,  
> you might prefer use a migration to change it to boolean, default false:
> 
> change\_column :\<table\_name\>, :has\_been\_read, :boolean, :default =\> false
> 
> This way, you can say
> 
> Post.find(:all, :conditions =\> ["has\_been\_read = ?, false])
> 
> And in your model, put something like
> 
> def mark\_as\_read  
> self.update\_attribute(‘has\_been\_read’, true)  
> end
> 
> Then you just call @thing.mark\_as\_read when it’s read, and you should  
> have everything you need.
> 
> –Al Evans
