Forum: Ruby on Rails Null Text Fields

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ian H. (Guest)
on 2005-12-20 18:18
(Received via mailing list)
I tend to allow nulls in cases where the value is unknown, even if
it's a text/varchar field.  RoR seems to push empty strings into these
fields.  Is there any way to get RoR to use NULL vice '' if the input
box is empty?

Thanks!

- Ian
Coey M. (Guest)
on 2006-06-07 23:28
Ian H. wrote:
> I tend to allow nulls in cases where the value is unknown, even if
> it's a text/varchar field.  RoR seems to push empty strings into these
> fields.  Is there any way to get RoR to use NULL vice '' if the input
> box is empty?
>
> Thanks!
>
> - Ian

I just found this problem myself.  The workaround I developed was to put
the following into the model object you want this to work for:

  before_validation :set_empty_to_nil

  def set_empty_to_nil
    self.attributes.each {|key, value|
      if value.instance_of?(String) and value == ''
        self["#{key}"] = nil
      end
    }
  end

Basically, everytime a create or update is performed, it walks through
all the attributes, and if its type is "String" and is thus empty, I set
it to nil.  (As I write this, I suppose I could have done "...
value.empty?", but anyhoo ...)

Of course, this means you have to put it into _every_ model object you
want this behavior for, so it sort of defeats the DRY principle.  There
may be a better solution (callback object?) other than modifying
ActiveRecord, but I'm not sure what that would be.  I also haven't
looked to see if this was filed as a bug or not.

Of course, part of the reason I did this as part of "before_validation"
rather than "before_save" was because I wanted to use a
"validates_uniqueness_of" and not have two empty strings declared equal.
But as it turns out, ActiveRecord won't allow two NULLs in a database as
it considers NULL = NULL (even though it specifically performs an "...
IS NULL" search).  So it was a bit of a moot point.

I can also see where a person may want '' saved to the database rather
than a NULL.  I'd just like the option of turning off that behavior.
Phillip K. (Guest)
on 2006-06-08 03:34
(Received via mailing list)
Coey M. wrote:
     before_validation :set_empty_to_nil

  def set_empty_to_nil
    self.attributes.each {|key, value|
      if value.instance_of?(String) and value == ''
        self["#{key}"] = nil
      end
    }
  end

Of course, this means you have to put it into _every_ model object you
want this behavior for, so it sort of defeats the DRY principle.  There
may be a better solution (callback object?) other than modifying
ActiveRecord, but I'm not sure what that would be.  Put this in a module
and mix in to each model you want the behavior for.

      I also haven't
looked to see if this was filed as a bug or not.  This seems like things
working as advertised to me, not a bug.
 If you have a form with fields left empty, those fields will still
post, they're just empty.
 And "" != nil in ruby -- the first is a String object, the second is
nil.

 So it's doing exactly what you told it to.

 -phil
This topic is locked and can not be replied to.