Forum: Ruby on Rails validation null objects and objects with data

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.
Craig W. (Guest)
on 2006-03-10 19:58
(Received via mailing list)
I have a model where initially, they we don't insert an gov_id value
because it isn't known.

The field is given a value of "" because it is on the form.

I then added code to the 'create' method to set that value to nil if it
is blank which seems to be fine but then it fails this validation (if
NULL)

  validates_length_of     :gov_id, :maximum => 18, :message => "Maximum
18 characters and numbers combined"

seems like I can't win here...because I can't pass validation uniqueness
if it is empty ("") and I can't pass validation length if it is NULL

Suggestions?

Craig
Ray B. (Guest)
on 2006-03-10 22:29
(Received via mailing list)
Craig W. wrote:
> 18 characters and numbers combined"
>
> seems like I can't win here...because I can't pass validation uniqueness
> if it is empty ("") and I can't pass validation length if it is NULL

Try validates_length_of :allow_nil ...

Ray
Craig W. (Guest)
on 2006-03-11 02:31
(Received via mailing list)
On Fri, 2006-03-10 at 12:25 -0800, Ray B. wrote:
> >   validates_length_of     :gov_id, :maximum => 18, :message => "Maximum
> > 18 characters and numbers combined"
> >
> > seems like I can't win here...because I can't pass validation uniqueness
> > if it is empty ("") and I can't pass validation length if it is NULL
>
> Try validates_length_of :allow_nil ...
>
----
that didn't work but
:allow_nil => true
did work

That wasn't offered in AWDWR book but is in the rails.api

thanks

Craig
This topic is locked and can not be replied to.