# :allow\_nil only working in Console?

**URL:** <https://www.ruby-forum.com/t/allow-nil-only-working-in-console/129783>\
**Category:** Rails\
**Created:** [February 21, 2008, 2:47pm UTC](https://www.ruby-forum.com/t/allow-nil-only-working-in-console/129783 "2008-02-21T14:47:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![joergbattermann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/edb3f5/32.png) [@joergbattermann](https://www.ruby-forum.com/u/joergbattermann)\
**Post date:** [February 21, 2008, 2:47pm UTC](https://www.ruby-forum.com/t/allow-nil-only-working-in-console/129783/1 "2008-02-21T14:47:41Z")

</div>

Hello there,

I have the following Book model, where users can enter the Title of  
the book and optionally the format of the ISBN-code shall be  
validated, but only if the user filled the ISBN field:

class Book \< ActiveRecord::Base

# associations

belongs\_to :user, :counter\_cache =\> true

# validations

validates\_presence\_of :title  
validates\_format\_of :isbn, :with =\> /[0-9-a-zA-Z]{10}|[0-9-a-zA-Z]  
{13}/, :allow\_nil =\> true  
validates\_existence\_of :user

attr\_protected :user\_id  
end

When I create a book on the console sans ISBN code, it works perfectly  
fine:

> > b = Book.new  
> > =\> #\<Book id: nil, title: nil, author: nil, isbn: nil, description:  
> > nil, user\_id: nil, created\_at: nil, updated\_at: nil\>
> 
> > b.user\_id = 1  
> > =\> 1
> 
> > b.title = “Book #1”  
> > =\> “Book #1”
> 
> > b.save  
> > =\> true

When I enter a non isbn-10 or isbn-13 code, it properly raises the  
error:

> > b.title = “Book #2”  
> > =\> “Book #2”
> 
> > b.isbn = “abc”  
> > =\> “abc”
> 
> > b.save  
> > =\> false
> 
> > b.errors  
> > =\> #\<ActiveRecord::Errors:0x2409564 @base=#\<Book id: nil, title: “Book  
> > #2”, author: nil, isbn: “abc”, description: nil, user\_id: 1,  
> > created\_at: nil, updated\_at: nil\>, @errors={“isbn”=\>[“is invalid”]}\>

So far so good. Now when I do the same in the browser & create an ISBN-  
less book, it says:

1 error prohibited this book from being saved  
There were problems with the following fields:  
Isbn is invalid

And the params looking like this:  
{“commit”=\>“Create”,  
“authenticity\_token”=\>“5c27df353e84da4378277ff8201bcacdb394a0d4”,  
“action”=\>“create”, “controller”=\>“books”, “user\_id”=\>“1”,  
“book”=\>{“isbn”=\>"", “title”=\>“abc”, “author”=\>"", “description”=\>""}}

Why is this? Shouldn’t :allow\_nil work there, too?

Or did I miss something?  
-J

---

<div class="post-metadata">

**Author:** ![joergbattermann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/edb3f5/32.png) [@joergbattermann](https://www.ruby-forum.com/u/joergbattermann)\
**Post date:** [February 21, 2008, 3:12pm UTC](https://www.ruby-forum.com/t/allow-nil-only-working-in-console/129783/2 "2008-02-21T15:12:06Z")

</div>

not sure, but most likely “isbn”=\>"" is an empty string and that for not  
nil

at least erb:  
“”.nil?  
=\> false

---

<div class="post-metadata">

**Author:** ![joergbattermann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/edb3f5/32.png) [@joergbattermann](https://www.ruby-forum.com/u/joergbattermann)\
**Post date:** [February 21, 2008, 5:25pm UTC](https://www.ruby-forum.com/t/allow-nil-only-working-in-console/129783/3 "2008-02-21T17:25:05Z")

</div>

Thorsten,

yeah - you’re right… didn’t expect it that way entirely… but a  
simple fix for this e.g. that one:  
[http://opensoul.org/2007/2/7/validations-on-empty-not-nil-attributes](http://opensoul.org/2007/2/7/validations-on-empty-not-nil-attributes)

On Feb 21, 3:12 pm, Thorsten M. \<rails-mailing-l…@andreas-
