# How to determine if a database column has been set?

**URL:** <https://www.ruby-forum.com/t/how-to-determine-if-a-database-column-has-been-set/153334>\
**Category:** Rails\
**Created:** [November 15, 2008, 1:05am UTC](https://www.ruby-forum.com/t/how-to-determine-if-a-database-column-has-been-set/153334 "2008-11-15T01:05:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 15, 2008, 1:05am UTC](https://www.ruby-forum.com/t/how-to-determine-if-a-database-column-has-been-set/153334/1 "2008-11-15T01:05:04Z")

</div>

lets say that i have a column in one of my tables:  
initial\_odometer\_reading (type is decimal), which for any particular  
record, may or may not be set by the user of the application.

somewhere in my code, i want to search for those records for which the  
user has set/initialized this column.

how do i do so, properly?

i can set the default value in the ‘create table’ method to be ‘-1’  
and then query for ‘-1’, this works, but doesn’t seem right. i want to  
do the right thing here.

what is the conventional way to solve this?

note that zero is a legitimate value to which the user may set the  
column.

thanks,  
yoram

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 15, 2008, 4:12am UTC](https://www.ruby-forum.com/t/how-to-determine-if-a-database-column-has-been-set/153334/2 "2008-11-15T04:12:21Z")

</div>

The database query that you want is foo.odometer IS NULL (for when it  
has not been set) or IS NOT NULL (for when it has ben set).

You could google ActiveRecord and “IS NULL” and see if you find hits. I  
_think_ the code does this correctly. e.g.

Table.find(:all, :conditions =\> { :odomiter =\> nil })

Try it, look at the log and see if you get the right query. Last  
resort, you can do the SELECT query yourself.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 15, 2008, 4:38am UTC](https://www.ruby-forum.com/t/how-to-determine-if-a-database-column-has-been-set/153334/3 "2008-11-15T04:38:43Z")

</div>

Also, if you are interested. NULL values in database tables, by the  
super DB guys like Date and Pascal, are frowned upon. They create  
“three way logic” which often yields unexpected results and is not part  
of boolean algebra and is outside of the theory that relational DB is  
based upon.

They would argue until they passed out that judicious use of default  
values is a far better way to go.
