Topic: Simple "validates_presence_of" question

Here's a newbie question...

In my database migration I have the following:

  t.column :value, :string, :null => false
  t.column :label, :string, :null => false

In my model I have the following:
  validates_presence_of :value, :label

Are these redundant (due to the presence of ":null => false")? Is it smart or silly to have both? Does it violate D-R-Y? Is there a Rails idiom that prefers one over the other?


Last edited by larry (2007-02-28 15:59:04)

Re: Simple "validates_presence_of" question

One difference is that validates_presence_of will check for a blank string as well as nil values. Another key difference is how the validation is handled by Rails.

When I first started with Rails I used to put ":null => false" everywhere. I just didn't want to deal with null/nil possibilities. But now I realize the importance of keeping all of this behavior out of the database and in Ruby. Handling validation in Ruby is much more flexible and powerful. Same goes for foreign key constraints, etc. Therefore, I recommend keeping the database validation/constraints in only one place: Ruby.

There are exceptions of course, such as if you frequently access the database outside of your Rails project. In that case I can understand duplicating this behavior in the database as well.

Right now, the only place a consistently use ":null => false" is for boolean columns. I don't want to have to check for both false and nil values in my queries. I will also add this if having a null value in the field will terribly break my application or cause some tricky bugs. Most of the time though, I leave that option off.

Railscasts - Free Ruby on Rails Screencasts

Re: Simple "validates_presence_of" question

Hey Ryan,

Thanks a lot - that is exactly the kind of answer I was looking for.

You mention using ":null => false" for boolean columns so you don't have to check for both false and nil values in your queries. Do you need to do that if you append a "?" to your query?

This is from the new Rails 1.2 book:

[i]To query a column as a boolean value in a condition, you must append a question mark to the column

Re: Simple "validates_presence_of" question

larry wrote:

Do you need to do that if you append a "?" to your query?

That works great, but in this case I need to do the check while I'm fetching the data from the database (in the SQL query). For example:

@inactive_users = User.find_all_by_active(false)

This won't work (IIRC) because the "active" column may be NULL so I need to do a check for that as well. I've also been bitten a few times with sorting issues. NULL is sorted differently than false/0 is in queries, so if you are sorting by a boolean column and some have null values then it won't behave as you expect.

Railscasts - Free Ruby on Rails Screencasts