I believe your problems are coming from the fact that you allow
something different than true/false in your DB column. If the column
is supposed to contain a boolean value, why are you allowing it to
contain the null value? A boolean value is either true or false, not
null. If I were you I would force the value to be one or the other and
make the column not null capable. You can enforce the rule with one of
the validation helpers. This is extracted from the
'validates_presence_of' documentation:

"If you want to validate the presence of a boolean field (where the
real values are true and false), you will want to use
validates_inclusion_of :field_name, :in => [true, false]. "

If you don't want to display errors and just control the issue
silently you can always use before_save to change the value to false
if needed. Something like:

<code>
def before_save
  self.my_boolean_column = false unless self.my_boolean_column == true
end
</code>

You should check against the value "true" (meaning that you shouldn't
leave the equality comparison out) because otherwise you could run
into problems if the value is ever something other than true/false/
null. A simple empty string would return "true" on the condition
otherwise as this example shows:

# This works in all cases
>> a = true
=> true
>> puts 'true' if a == true
true
=> nil
>> puts 'true' if a
true
=> nil

# This works only when comparing against 'true'
>> a = ''
=> ""
>> puts 'true' if a == true
=> nil
>> puts 'true' if a
true  # Notice how this would make the code above work incorrectly on
the before_save
=> nil

On Nov 18, 3:59 pm, David Cross <dcrosst...@gmail.com> wrote:
> I have a boolean column that I would like to modify with a select
> helper element, and have :include_blank map to (database) null.
>
> This *almost* works.  On the create/edit/update page, if you select
> true/yes it sets the column to true, if you select No/false it sets it
> to false, and if you set it to blank, it sets it to (database) null.
> fantastic.
>
> Now you edit the record.  If it is "True" in the database the select
> box picks the correct value.  If it was set to 'no' or (database)null
> however the selection is set to the blank value.
>
> This is especially problematic if you edit any other field on the
> form, as it resets *everything* back to 'null', regardless of if it
> was "false" or "null" to beginwith.
>
> I have constructed a trivial test application to demonstrate this:
>
> http://www.cs.rpi.edu/~crossd/selecttest.tar.bz2
>
> Fire it up in 'rails server' and go tohttp://127.0.0.1:3000/testings
>
> (I've included a sqlite3 db there with 2 rows in it).  edit one, set
> it to 'Yes', check the 'show' page, edit it again, set it to 'No',
> check the 'show' page, edit it again, set it to blank (you won't have
> to, its already there), and save it and check the show page. notice
> all 3 edits work correctly, but the initial value is NOT set correctly
> for for the 'False' case.
>
> Is this a bug? am I doing something wrong?
> --
> David E. Cross

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to