On May 7, 6:11 am, Ben <[email protected]> wrote:
> I have a form with a lot of optional fields, and my params hash
> contains all of those empty fields as
>
> { :key1 => "", :key2 => "", etc... }
>
> I want those to be treated as nil, so that in the db they're NULL, and
> so that I can easily check with .nil?
>
> I searched around the groups and documentation, but the closest thing
> I've found is typecast_empty_string_to_nil for non-string fields.
>
> Does sequel provide any way to convert empty strings to nil, short of
> manually overriding the setter methods or modifying the params hash?
> How would you solve this problem?
You can override Model#typecast_value to do that:
class Sequel::Model
private
def typecast_value(column, value)
value = nil if value == ''
super
end
end
Jeremy
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sequel-talk?hl=en
-~----------~----~----~----~------~----~------~--~---