views:

129

answers:

2

I have a webform created in Rails that asks if someone has children or not? If I submit the form, it's able to get inserted into the db. When I edit the form, it doesn't show it ask selected.

For example, the field is

<%= f.select :have_children, [ ["Yes", 0"], ["No", 1] ] %>

Whenever I select "No" and then edit the form again, it shows this field as "Yes." In the db, it's saved as value 1. Is it because the form can't associate values 0 or 1 with "Yes" or "No"? Am I missing something in the form?

+3  A: 

I'm assuming you're using MySql as your database?

If so, it's because mysql doesn't have a bool type. Instead it uses TINYINT and sets 1 to true and 0 to false.

When you reload the data back into your web-form it should work fine, but I'd advise that you just make yes = 1 and no = 0 - this is the "standard" and going against it for no reason is just going to cause confusion and suffering in future.

Orion Edwards
PS: This is based on my experience with mysql which may be out of date. If it does actually have a dedicated bool type, or I've just been wrong, sorry
Orion Edwards
+1, and on a slightly different note, when you're starting out, you should work with the Rails magic rather than against it. So true should be 1, false should be 0 - otherwise a lot of things may not work how you expect.
Sarah Mei
+2  A: 

Why are you even using a select for this at all? A simple checkbox would suffice.

Ryan Bigg