Topic: Set a database field to null

How do I set a database field to null?

Re: Set a database field to null

use migration to set it to default to NULL

Re: Set a database field to null

But what if I have a field which is not null and I want to revert it to null programmatically?

Re: Set a database field to null

what datatype is the field

Re: Set a database field to null

evamedia wrote:

what datatype is the field

It's a MS SQL int.

Re: Set a database field to null

why would you want a null, and not a zero in an int field, just curious

Re: Set a database field to null

If I have a 0 will it behave as NULL in ruby? I'd like to be able to do .nil?

Re: Set a database field to null

no,

not a ruby expert, but I assume you can do

if variable == 0

Re: Set a database field to null

You can set it to NULL by setting it to nil in ruby:

@person.foo = nil
@person.save

Railscasts - Free Ruby on Rails Screencasts

10

Re: Set a database field to null

ryanb wrote:

You can set it to NULL by setting it to nil in ruby:

@person.foo = nil
@person.save

Excellent. Thanks ryan.