RubyFlow The Ruby and Rails community linklog

×

The Ruby and Rails community linklog

Made a library? Written a blog post? Found a useful tutorial? Share it with the Ruby community here or just enjoy what everyone else has found!

Remove leading and trailing blanks from attr vals in Rails models

Somewhat like nillify_blanks, stripper converts blank field values to nil. However, it also removes (ie strips) leading and trailing blanks from the value first. Another distinction is that it performs this stripping whether or not the column allows nulls–I’m from the school that believes an empty string makes no sense as the value for a column marked as NOT NULL :).

Comments

It should be noted that some RDBM’s automatically convert empty strings to NULL anyway or are configurable.

I’m guessing MySQL doesn’t and isn’t.

No, MySQL doesn’t. I don’t know if it’s configurable to do so, although I’d personally dislike it as nil != “”

The only database that I’m aware of that does is Oracle and, ironically, I don’t believe that it should be doing that :)

I’m glad Oracle does it. It annoys the Java “programmers” at work to no end because they have to deal with Null vs String.

Naturally they get around it by inserting a single blank space, which is a joy to deal with.

Post a comment

You can use basic HTML markup (e.g. <a>) or Markdown.

As you are not logged in, you will be
directed via GitHub to signup or sign in