# Validates\_length\_of not working with :if?

**URL:** <https://www.ruby-forum.com/t/validates-length-of-not-working-with-if/90500>\
**Category:** Rails\
**Created:** [March 29, 2007, 10:47am UTC](https://www.ruby-forum.com/t/validates-length-of-not-working-with-if/90500 "2007-03-29T10:47:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [March 29, 2007, 10:47am UTC](https://www.ruby-forum.com/t/validates-length-of-not-working-with-if/90500/1 "2007-03-29T10:47:48Z")

</div>

Hello,

I’m trying to cut down the errors if the guy already getting the empty  
nickname, so he doesn’t get anything about the nickname is short.

validates\_presence\_of :nickname

This below is not working ?

validates\_length\_of :nickname, :within =\> 4…40, :if =\> Proc.new {  
|user| user.nickname.length \> 1 }

nor this one below?

validates\_length\_of :nickname, :within =\> 4…40, :if =\> nickname.length

> 1

Anyone have a solution for this?

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [March 29, 2007, 7:28pm UTC](https://www.ruby-forum.com/t/validates-length-of-not-working-with-if/90500/2 "2007-03-29T19:28:43Z")

</div>

validates\_presence\_of :nickname  
validates\_length\_of :nickname, :in =\> 4…40, :allow\_nil =\> true

but just make sure that you change the nickname param from “” [empty  
string]  
to nil or it won’t work. “”.nil? =\> false. I dunno why there’s not a  
:allow\_blank =\> true which could cover nil? and empty?

RSL

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [March 30, 2007, 12:18am UTC](https://www.ruby-forum.com/t/validates-length-of-not-working-with-if/90500/3 "2007-03-30T00:18:06Z")

</div>

Russell N. wrote:

> validates\_presence\_of :nickname  
> validates\_length\_of :nickname, :in =\> 4…40, :allow\_nil =\> true
> 
> but just make sure that you change the nickname param from “” [empty  
> string]  
> to nil or it won’t work. “”.nil? =\> false. I dunno why there’s not a  
> :allow\_blank =\> true which could cover nil? and empty?
> 
> RSL

Hmm, thats kind of weird, in the Web D. with Rails the book  
give a example to this.

validates\_length\_of :nickname, :in =\> 4…40, :if =\> Proc.new {|u|  
u.nickname.blank?

But this are not working?

Your example are not working either, it gives me “undefined local  
variable or method ‘allow\_nil’” ?

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [March 30, 2007, 12:28am UTC](https://www.ruby-forum.com/t/validates-length-of-not-working-with-if/90500/4 "2007-03-30T00:28:52Z")

</div>

Russell N. wrote:

> Perhaps you have other errors? I dunno how else to help. Here’s the API  
> page  
> for validates\_length\_of which clearly states that :allow\_nil is a valid  
> parameter.
> 
> [ActiveRecord::Validations::ClassMethods](http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M000942)
> 
> Sigh.
> 
> RSL

Yeah, I’m wondering about that too!

Kind of weird ☹

But thanks for your help 😃

---

<div class="post-metadata">

**Author:** ![jamal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jamal](https://www.ruby-forum.com/u/jamal)\
**Post date:** [March 30, 2007, 12:27am UTC](https://www.ruby-forum.com/t/validates-length-of-not-working-with-if/90500/5 "2007-03-30T00:27:24Z")

</div>

Perhaps you have other errors? I dunno how else to help. Here’s the API  
page  
for validates\_length\_of which clearly states that :allow\_nil is a valid  
parameter.

[http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M000942](http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M000942)

Sigh.

RSL
