I have the following code for a select box in a form:
<div class="field">
<%= f.label :user_id %>
<%= f.collection_select :user_id, User.all, :id, :name, include_blank: true %>
</div>
And it displays the following select options:
I noticed that when the blank option is selected, what actually gets saved in the database is an empty string: ''
I do not want to save an empty string when the blank option is selected. Instead: I want to save null.
I attempted overriding the setter in the rails model but it didn't work:
def text_value=(value)
write_attribute(:name, nil) and return if value.empty?
super
end
How might I do this at the application level? I am aware that I could have a Default Value specified at the Database level, but it seems that this might be better suited at the application level.

null. Pretend that there is another select option that specifies 'does not apply' or 'no' and which actually saves a blank string value. Saving it asnullin the database is just a way for me to say 'oh ok, they did not answer this question at all.