Hi All, I hope somebody can help.
I'm a bit confused about what would be the best practice for handling an enumerated model value with FormOptionsHelper.select + validates_inclusion_of. Specifically the nil handling.
Some background...
I have an enumerated set of values, which I'm validating using validates_inclusion_of. Gender (like in the Rails docs example) is something I have to tackle, but additionally nil is allowed - as there will be no gender information supplied at initial user registration:
validates_inclusion_of :gender, :in => %w( m f ), :allow_nil => true
An edit profile form will allow gender to be chosen later. In this form, I'm using FormOptionsHelper's select with :prompt, so that a user is prompted to make a choice if they have not done so yet. i.e. the model's underlying gender value is nil. For situations where a gender has already been chosen, just the male / female choice is preferred (hence :prompt seemed a better match to requirements than :include_blank which always provides a blank option).
The problem is that the gender field "prompt" value gets passed through for the gender parameter as blank ("") when no selection is made, but I'd prefer the value to be left at nil. Rails implementation considerations aside, if I had a nil value in the model before, and made no change, I'd expect to retain a nil value after.
So, is :allow_nil of more limited use in validations, for the case where no input field ever updates the value?
Is it better to opt for :allow_blank => true on any validation where values are supplied via web form? (I take it that :allow_blank accepts "" and nil as the blank? method does)
Does anybody go to the effort of translating blank parameter values back to nils after form submission - and is it worth it?
Any help would be gratefully received. I can't actually believe I never stumbled over this one before! I guess enumerated values just feel a bit different from regular text values, as nil & blank never bothered me for those. I'm using Rails 2.1 & Ruby 1.8.7
Thanks!