On Sunday, December 2, 2018 at 7:55:00 PM UTC-8, craig buchanan wrote:
>
> Is there a way to automatically convert empty string to nils during mass
> assignment?
>
> Currently:
>
> irb(main):016:0> c = {first_name:'micky',last_name:'mouse',title:''}=> {:
> first_name=>"micky", :last_name=>"mouse", :title=>""}
>
> irb(main):017:0> Contact.new(c)=> #<Contact
> @values={:first_name=>"micky", :last_name=>"mouse", :title=>""}>
>
> While it's not a big deal to store the empty strings, I'd prefer not to do
> so.
>
Assuming you don't want a fix specific to mass-assignment:
def title=(v)
v = nil if '' == v
super
end
Sequel typecasts empty strings to nil for non-string/blob columns by
default, but does not do the same for string/blob columns as NULL and ''
are different (unless you are using Oracle).
If you wanted to make such a change globally:
class Sequel::Model
def typecast_value(column, value)
value = nil if '' == value
super
end
end
Thanks,
Jeremy
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.