# Select helper :prompt storing 0 in database

**URL:** <https://www.ruby-forum.com/t/select-helper-prompt-storing-0-in-database/67934>\
**Category:** Rails\
**Created:** [September 8, 2006, 1:11am UTC](https://www.ruby-forum.com/t/select-helper-prompt-storing-0-in-database/67934 "2006-09-08T01:11:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![klancaster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f1d935/32.png) [@klancaster](https://www.ruby-forum.com/u/klancaster)\
**Post date:** [September 8, 2006, 1:11am UTC](https://www.ruby-forum.com/t/select-helper-prompt-storing-0-in-database/67934/1 "2006-09-08T01:11:04Z")

</div>

Have to be missing something simple here…

The select helper methods include the option to dynamically add a  
“Please select” item or simply a blank to the top of the list, as in

select(“post”, “category”, Post::CATEGORIES, {:include\_blank =\> true})

When this field is not required and a user leaves it as is and submits  
the form, it sends back an empty string to the controller, which  
unfortunately gets converted to a 0 if the field type is an integer (as  
in the case of a foreign key). As a result, you get a 0 in the database  
instead of a null, so that when you invoke the form again, the first  
value in the dropdown is selected - obviously incorrect. The underlying  
problem in part is that “”.to\_i and nil.to\_i both return 0. (BTW, the  
database columns in question _can_ accept nulls)

We are currently doing a hack to remove the values in the controller  
action, but is there a better way?

TIA,  
Keith

---

<div class="post-metadata">

**Author:** ![klancaster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f1d935/32.png) [@klancaster](https://www.ruby-forum.com/u/klancaster)\
**Post date:** [September 9, 2006, 11:10pm UTC](https://www.ruby-forum.com/t/select-helper-prompt-storing-0-in-database/67934/2 "2006-09-09T23:10:57Z")

</div>

So…based on the overwhelming response… nobody else seeing this as  
an issue? Removing the offending values in the controller is the best  
way to go?

Keith

---

<div class="post-metadata">

**Author:** ![klancaster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f1d935/32.png) [@klancaster](https://www.ruby-forum.com/u/klancaster)\
**Post date:** [September 10, 2006, 1:37am UTC](https://www.ruby-forum.com/t/select-helper-prompt-storing-0-in-database/67934/3 "2006-09-10T01:37:43Z")

</div>

On 9/7/06, Keith L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> unfortunately gets converted to a 0 if the field type is an integer (as  
> in the case of a foreign key). As a result, you get a 0 in the database  
> instead of a null, so that when you invoke the form again, the first  
> value in the dropdown is selected - obviously incorrect. The underlying  
> problem in part is that “”.to\_i and nil.to\_i both return 0. (BTW, the  
> database columns in question _can_ accept nulls)
> 
> We are currently doing a hack to remove the values in the controller  
> action, but is there a better way?

Can’t use a validation in your controller for this field?

---

<div class="post-metadata">

**Author:** ![klancaster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f1d935/32.png) [@klancaster](https://www.ruby-forum.com/u/klancaster)\
**Post date:** [September 10, 2006, 2:37am UTC](https://www.ruby-forum.com/t/select-helper-prompt-storing-0-in-database/67934/4 "2006-09-10T02:37:01Z")

</div>

Michael C. wrote:

> On 9/9/06, Michael C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > the form, it sends back an empty string to the controller, which
> > 
> > Can’t use a validation in your controller for this field?
> 
> Gah, of course I mean “model” there, not “controller”.

We can, IF we do custom validation. Part of the problem (which we’ve  
seen in other areas) is that Ruby coerces nil or blank strings (“”) to  
0. The standard validator, for example, validates\_numericality\_of, will  
let a 0 (zero) go through just fine.

Keith

---

<div class="post-metadata">

**Author:** ![klancaster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f1d935/32.png) [@klancaster](https://www.ruby-forum.com/u/klancaster)\
**Post date:** [September 10, 2006, 3:15am UTC](https://www.ruby-forum.com/t/select-helper-prompt-storing-0-in-database/67934/5 "2006-09-10T03:15:54Z")

</div>

On 9/9/06, Keith L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> seen in other areas) is that Ruby coerces nil or blank strings (“”) to  
> 0. The standard validator, for example, validates\_numericality\_of, will  
> let a 0 (zero) go through just fine.

Still, that shouldn’t be too difficult, right?

def validate  
if self.whatever == 0  
…

---

<div class="post-metadata">

**Author:** ![klancaster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f1d935/32.png) [@klancaster](https://www.ruby-forum.com/u/klancaster)\
**Post date:** [September 10, 2006, 1:43am UTC](https://www.ruby-forum.com/t/select-helper-prompt-storing-0-in-database/67934/6 "2006-09-10T01:43:44Z")

</div>

On 9/9/06, Michael C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > the form, it sends back an empty string to the controller, which
> 
> Can’t use a validation in your controller for this field?

Gah, of course I mean “model” there, not “controller”.

---

<div class="post-metadata">

**Author:** ![klancaster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f1d935/32.png) [@klancaster](https://www.ruby-forum.com/u/klancaster)\
**Post date:** [September 10, 2006, 5:48am UTC](https://www.ruby-forum.com/t/select-helper-prompt-storing-0-in-database/67934/7 "2006-09-10T05:48:10Z")

</div>

On 9/9/06, Keith L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > def validate  
> > if self.whatever == 0  
> > …
> 
> True. Guess that’s what we will do - seems cleaner than trapping things  
> in the controller the way we have been , although still seems a bit  
> clunky…

I’m still not completely comfortable with validations in the model; no  
reason for that, just my MVC upbringing that always did that sort of  
thing in the controller. I’m getting used to it though.

> Anyway, thanks for the assist.

Glad I could help!

---

<div class="post-metadata">

**Author:** ![klancaster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f1d935/32.png) [@klancaster](https://www.ruby-forum.com/u/klancaster)\
**Post date:** [September 10, 2006, 3:26am UTC](https://www.ruby-forum.com/t/select-helper-prompt-storing-0-in-database/67934/8 "2006-09-10T03:26:14Z")

</div>

Michael C. wrote:

> On 9/9/06, Keith L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > seen in other areas) is that Ruby coerces nil or blank strings (“”) to  
> > 0. The standard validator, for example, validates\_numericality\_of, will  
> > let a 0 (zero) go through just fine.
> 
> Still, that shouldn’t be too difficult, right?
> 
> def validate  
> if self.whatever == 0  
> …

True. Guess that’s what we will do - seems cleaner than trapping things  
in the controller the way we have been , although still seems a bit  
clunky…

Anyway, thanks for the assist.

Keith
