<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>opensoul.org - Validations on empty (not nil) attributes Comments</title>
  <id>tag:opensoul.org,2008:/2007/2/7/validations-on-empty-not-nil-attributes/comments</id>
  <generator version="0.8.0" uri="http://mephistoblog.com">Mephisto Drax</generator>
  <link href="http://opensoul.org/2007/2/7/validations-on-empty-not-nil-attributes/comments.xml" rel="self" type="application/atom+xml"/>
  <link href="/2007/2/7/validations-on-empty-not-nil-attributes" rel="alternate" type="text/html"/>
  <updated>2008-06-17T19:27:07Z</updated>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>neil</name>
    </author>
    <id>tag:opensoul.org,2007-02-07:1003:6528</id>
    <published>2008-06-17T19:27:07Z</published>
    <updated>2008-06-17T19:27:07Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/2/7/validations-on-empty-not-nil-attributes" rel="alternate" type="text/html"/>
    <title>Comment on 'Validations on empty (not nil) attributes' by neil</title>
<content type="html">&lt;p&gt;Since I definitely did need to allow for a few not-null columns, but I wanted all the rest of my columns (in a large, dynamic schema) to always be nil if empty, I modified the plugin slightly to check the column definition to make sure it allows null, before converting to nil:&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;def clear_empty_attrs
  # set any empty-string attributes to nil if the column allows null
  @attributes.each do |key,value|
    self[key] = nil if value.blank? &#38;&#38; self.class.columns_hash[key].null
  end
end&lt;/code&gt;&lt;/pre&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>andres</name>
    </author>
    <id>tag:opensoul.org,2007-02-07:1003:6179</id>
    <published>2008-05-21T15:25:22Z</published>
    <updated>2008-05-21T15:25:22Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/2/7/validations-on-empty-not-nil-attributes" rel="alternate" type="text/html"/>
    <title>Comment on 'Validations on empty (not nil) attributes' by andres</title>
<content type="html">&lt;p&gt;well, there&#8217;s built in
:allow_blank =&amp;gt; true
to use instead or alongside with allow_nil&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Marcello Golfieri</name>
    </author>
    <id>tag:opensoul.org,2007-02-07:1003:5392</id>
    <published>2008-03-03T16:53:41Z</published>
    <updated>2008-03-03T16:53:41Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/2/7/validations-on-empty-not-nil-attributes" rel="alternate" type="text/html"/>
    <title>Comment on 'Validations on empty (not nil) attributes' by Marcello Golfieri</title>
<content type="html">&lt;p&gt;Yeah, actually my solution does work flawlessy in each scenario  without any of the eventual drawbacks cited here. Nevertheless, having a dedicated :allow_nil would definitely be welcome.  In fact, even if adding 3 characters to the regexp looks even faster than typing &#8221;:allow_nil =&amp;gt; true&#8221;, it does create some confusion on newcomers.  Adding the option :allow_nil here too would standardize its use; people obviously expects that option for validates_format_of too (I was one of those, darn it!)
Last but not least,  &#8220;Convention&#8221; is cooler than &#8220;Configuration&#8221;, isn&#8217;t it?...  (I just wonder where I got this from&#8230; ;-)
Should be added to next Rails&#8217; wish list.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Joerg Battermann</name>
    </author>
    <id>tag:opensoul.org,2007-02-07:1003:5337</id>
    <published>2008-02-21T16:23:46Z</published>
    <updated>2008-02-21T16:23:46Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/2/7/validations-on-empty-not-nil-attributes" rel="alternate" type="text/html"/>
    <title>Comment on 'Validations on empty (not nil) attributes' by Joerg Battermann</title>
<content type="html">&lt;p&gt;Thanks! Solved my problem with nil != &#8221;&#8221; for strings as mentioned here: http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/145fcc2701653186/8d5b8acdf688e681#8d5b8acdf688e681&lt;/p&gt;


	&lt;p&gt;:)&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Marcello Golfieri</name>
    </author>
    <id>tag:opensoul.org,2007-02-07:1003:5190</id>
    <published>2008-02-01T16:56:32Z</published>
    <updated>2008-02-01T16:56:32Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/2/7/validations-on-empty-not-nil-attributes" rel="alternate" type="text/html"/>
    <title>Comment on 'Validations on empty (not nil) attributes' by Marcello Golfieri</title>
<content type="html">&lt;p&gt;How about prepending  &#8221;^$|&#8221;  to the regexp?&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Kai Krakow</name>
    </author>
    <id>tag:opensoul.org,2007-02-07:1003:3431</id>
    <published>2007-08-23T13:23:18Z</published>
    <updated>2007-08-23T13:23:18Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/2/7/validations-on-empty-not-nil-attributes" rel="alternate" type="text/html"/>
    <title>Comment on 'Validations on empty (not nil) attributes' by Kai Krakow</title>
<content type="html">&lt;p&gt;Brandon,&lt;/p&gt;


	&lt;p&gt;[quote]
What is your intention of defining the type column as “NOT &lt;span class=&quot;caps&quot;&gt;NULL&lt;/span&gt;” (forcing that column to have a value) if, without this plugin, there is absolutely no way to create a record through Rails with the type column being set to &lt;span class=&quot;caps&quot;&gt;NULL&lt;/span&gt;? Seems like you could just remove that constraint.
[/quote]&lt;/p&gt;


	&lt;p&gt;Well Rails allows to create records with this constraint active by the way it just sets the column to empty string. Of course this is suppressed by your plugin. But you are right: This constraint wasn&#8217;t optimal, it was just the default of the software I used to create this column. ;-)&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Brandon</name>
    </author>
    <id>tag:opensoul.org,2007-02-07:1003:3404</id>
    <published>2007-08-19T14:51:12Z</published>
    <updated>2007-08-19T14:51:12Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/2/7/validations-on-empty-not-nil-attributes" rel="alternate" type="text/html"/>
    <title>Comment on 'Validations on empty (not nil) attributes' by Brandon</title>
<content type="html">&lt;p&gt;Kai,&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;It look’s like Rails does not use the name of the base class for the type columns but instead an empty string which was set to nil by your plugin.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;What is your intention of defining the type column as &#8220;NOT &lt;span class=&quot;caps&quot;&gt;NULL&lt;/span&gt;&#8221; (forcing that column to have a value) if, without this plugin, there is absolutely no way to create a record through Rails with the type column being set to &lt;span class=&quot;caps&quot;&gt;NULL&lt;/span&gt;?  Seems like you could just remove that constraint.&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;I think the real power in this plugin is on validating uniqueness and not what your original intend was (validates_format_of).&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;The intent of this plugin is that I don&#8217;t want empty values spread all over my database. In 99% of cases, I don&#8217;t care about empty values and want them treated as null.  In the other 1%, I don&#8217;t use this plugin and copy the code out of it to customize it.  It&#8217;s only a couple lines&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Kai Krakow</name>
    </author>
    <id>tag:opensoul.org,2007-02-07:1003:3402</id>
    <published>2007-08-19T10:16:40Z</published>
    <updated>2007-08-19T10:16:40Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/2/7/validations-on-empty-not-nil-attributes" rel="alternate" type="text/html"/>
    <title>Comment on 'Validations on empty (not nil) attributes' by Kai Krakow</title>
<content type="html">&lt;p&gt;Brandon,&lt;/p&gt;


	&lt;p&gt;My type column was defined &#8220;NOT &lt;span class=&quot;caps&quot;&gt;NULL&lt;/span&gt;&#8221; but was set to null if I stored the base class in the table. It look&#8217;s like Rails does not use the name of the base class for the type columns but instead an empty string which was set to nil by your plugin. Would it be possible to extend the plugin to only touch fields that Rails validates_uniqueness_of? I think the real power in this plugin is on validating uniqueness and not what your original intend was (validates_format_of).&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Brandon</name>
    </author>
    <id>tag:opensoul.org,2007-02-07:1003:3396</id>
    <published>2007-08-17T15:14:08Z</published>
    <updated>2007-08-17T15:14:08Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/2/7/validations-on-empty-not-nil-attributes" rel="alternate" type="text/html"/>
    <title>Comment on 'Validations on empty (not nil) attributes' by Brandon</title>
<content type="html">&lt;p&gt;Kai,&lt;/p&gt;


	&lt;p&gt;I&#8217;m not sure that I follow you.  I&#8217;m using the plugin in several projects that use &lt;span class=&quot;caps&quot;&gt;STI&lt;/span&gt;.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Kai</name>
    </author>
    <id>tag:opensoul.org,2007-02-07:1003:3358</id>
    <published>2007-08-11T09:29:51Z</published>
    <updated>2007-08-11T09:29:51Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/2/7/validations-on-empty-not-nil-attributes" rel="alternate" type="text/html"/>
    <title>Comment on 'Validations on empty (not nil) attributes' by Kai</title>
<content type="html">&lt;p&gt;Very nice work. I solved the very same problem with validates_uniqueness_of &#8230;, :allow_nil =&amp;gt; true with this. But I at once ran in to another problem: I am using this validation on a set of single inheritance tables. One can no longer use the &#8220;master&#8221; class with your plugin because the type column is set nil instead of empty. Your plugin should be enhanced to not touch the type column.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Eldon</name>
    </author>
    <id>tag:opensoul.org,2007-02-07:1003:1093</id>
    <published>2007-02-13T06:57:38Z</published>
    <updated>2007-02-13T06:57:38Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/2/7/validations-on-empty-not-nil-attributes" rel="alternate" type="text/html"/>
    <title>Comment on 'Validations on empty (not nil) attributes' by Eldon</title>
<content type="html">&lt;p&gt;Just came across your posting and actually i think the problem is that you&#8217;re trying to use the allow_nil option on a method that it&#8217;s not designed to go with.&lt;/p&gt;


	&lt;p&gt;Check the &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; and you&#8217;ll see that there&#8217;s not an allow_nil option for the validates_format_of method.&lt;/p&gt;


	&lt;p&gt;In fact the only reason it doesn&#8217;t throw an error is because internally validates_format_of also calls the validates_each method which &lt;span class=&quot;caps&quot;&gt;DOES&lt;/span&gt; have the allow_nil option but that&#8217;s still an incorrect use of the method.&lt;/p&gt;


	&lt;p&gt;I solved the same problem a while back by doing it like this.&lt;/p&gt;


	&lt;p&gt;I added a validates format on multiple optional &lt;span class=&quot;caps&quot;&gt;SSN&lt;/span&gt; fields on a page like this:
validates_format_of :byr1ssn1,:byr1ssn2,:slr1ssn1, :slr1ssn2, :with =&amp;gt; /(&lt;sup class=&quot;\d&quot;&gt;?(\d{2})[&lt;/del&gt;\s.]?(\d{4})$)|&lt;/sup&gt;$/, :message =&amp;gt; &#8216;Not Recognized as a Valid &lt;span class=&quot;caps&quot;&gt;SSN&lt;/span&gt;&#8217;&lt;/p&gt;


	&lt;p&gt;This allows the end user to input anything that looks like a &lt;span class=&quot;caps&quot;&gt;SSN&lt;/span&gt; and I accept it (i.e. they can format it any way that&#8217;s convenient for them &#8211; spaces, dashes, etc). If they leave this blank &#8211; no errors are thrown.&lt;/p&gt;


	&lt;p&gt;Then since I want all &lt;span class=&quot;caps&quot;&gt;SSN&lt;/span&gt;&#8217;s in my database to be in a specific format &#8211; i added a before save method&lt;/p&gt;


	&lt;p&gt;before_save :convert_ssns&lt;/p&gt;


	&lt;p&gt;which just does this&lt;/p&gt;


	&lt;pre&gt;&lt;code&gt;def convert_ssns
  regex  = /^(\d{3})[&lt;del&gt;\s.]?(\d{2})[&lt;/del&gt;\s.]?(\d{4})$/
  ssns = :byr1ssn1,:byr1ssn2,:slr1ssn1, :slr1ssn2
  for ssn in ssns
    r =  regex.match(self[ssn])
    if r
      self[ssn] = &quot;#{r[1]}&lt;del&gt;#{r[2]}&lt;/del&gt;#{r[3]}&quot; 
    end  
  end
end&lt;/code&gt;&lt;/pre&gt;


	&lt;p&gt;So I get the best of all worlds &#8211; an optional &lt;span class=&quot;caps&quot;&gt;SSN&lt;/span&gt; field, easier input for end-users, and a consistent standard for the data in the database. And yeah looking at that convert_ssns method now &#8211; I can see that I should probably go back and refactor it a little as it&#8217;s a bit verbose.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Justin</name>
    </author>
    <id>tag:opensoul.org,2007-02-07:1003:1008</id>
    <published>2007-02-08T03:49:06Z</published>
    <updated>2007-02-08T03:49:06Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/2/7/validations-on-empty-not-nil-attributes" rel="alternate" type="text/html"/>
    <title>Comment on 'Validations on empty (not nil) attributes' by Justin</title>
<content type="html">&lt;p&gt;It does make sense in a round-a-bout kind of way &#8211; if you don&#8217;t specify the default value to be null, then both the database and rails are expecting a string, even if it is an empty one.&lt;/p&gt;


	&lt;p&gt;Whether Rails picks up on this or not is another questions, I&#8217;m not currently somewhere I can test this out. I had a similar problem with strings and nil previously, and it&#8217;s what somebody mentioned in #rubyonrails.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Justin</name>
    </author>
    <id>tag:opensoul.org,2007-02-07:1003:1007</id>
    <published>2007-02-08T03:42:24Z</published>
    <updated>2007-02-08T03:42:24Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/2/7/validations-on-empty-not-nil-attributes" rel="alternate" type="text/html"/>
    <title>Comment on 'Validations on empty (not nil) attributes' by Justin</title>
<content type="html">&lt;p&gt;II&#8217;m not 100% sure, but I believe you may be able to fix this in your schema. I think if you set the default value of the DB field to null, then rails will typecast empty entries to nil as expected.&lt;/p&gt;


	&lt;p&gt;Not 100% sure though, worth a try.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Brandon</name>
    </author>
    <id>tag:opensoul.org,2007-02-07:1003:1006</id>
    <published>2007-02-07T14:54:47Z</published>
    <updated>2007-02-07T14:54:47Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/2/7/validations-on-empty-not-nil-attributes" rel="alternate" type="text/html"/>
    <title>Comment on 'Validations on empty (not nil) attributes' by Brandon</title>
<content type="html">&lt;p&gt;You&#8217;re right, the plugin is not entirely neccesary.  However, I think it is a better solution.&lt;/p&gt;


	&lt;p&gt;ActiveRecord performs very transparent type-casting.  Dates that come in as strings get automatically cast to Date; numbers get cast to Numeric.  In my opinion, empty strings should get cast to nil.&lt;/p&gt;


	&lt;p&gt;I can&#8217;t think of a use-case where I would want to store and use and empty string value.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>K. Adam Christensen</name>
    </author>
    <id>tag:opensoul.org,2007-02-07:1003:1005</id>
    <published>2007-02-07T13:35:22Z</published>
    <updated>2007-02-07T13:35:22Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/2/7/validations-on-empty-not-nil-attributes" rel="alternate" type="text/html"/>
    <title>Comment on 'Validations on empty (not nil) attributes' by K. Adam Christensen</title>
<content type="html">&lt;p&gt;Excuse my rudeness with the first comment.&lt;/p&gt;


	&lt;p&gt;Your plugin does make sense because there is the parameter &lt;code&gt;:allow_nil&lt;/code&gt; to use.  This certainly allows for that usage in the context of what you have.&lt;/p&gt;


	&lt;p&gt;That aside, I don&#8217;t know if that is important to warrant the use of this plugin.  The example I gave performs the check in the context of what you were after.&lt;/p&gt;


	&lt;p&gt;What if your first form does not contain your social security number?  Then certainly the &lt;code&gt;:allow_nil&lt;/code&gt; option makes sense because there will not be an empty form field to pollute.  Now say you have another form, which does ask for the social security number.  Now your validation will fire off.  The &lt;code&gt;:allow_nil&lt;/code&gt; can almost be seen as &#8220;validate this when I ask for it&#8221;.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>K. Adam Christensen</name>
    </author>
    <id>tag:opensoul.org,2007-02-07:1003:1004</id>
    <published>2007-02-07T12:55:21Z</published>
    <updated>2007-02-07T12:55:21Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/2/7/validations-on-empty-not-nil-attributes" rel="alternate" type="text/html"/>
    <title>Comment on 'Validations on empty (not nil) attributes' by K. Adam Christensen</title>
<content type="html">&lt;p&gt;The other thing you could have done was:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
class Person &amp;lt; ActiveRecord::Base
  validates_format_of :social_security_number,
    :with =&amp;gt; /\d{3}[-]?\d{2}[-]?\d{4}/, :if =&amp;gt; :social_security_number?
end
&lt;/code&gt;
&lt;/pre&gt;</content>  </entry>
</feed>
