This answer to Rails: Force empty string to NULL in the database suggests using a mixin to set empty string column values to nil.
Is there a way to test if the column accepts nil/null values?
> f = Foobar.find(1)
=> #<Entity id: 7, name: "foo", description: nil, has_bar: false, created_at: "2014-08-04 21:50:42", updated_at: "2014-08-05 19:35:33">
> f.has_bar.allow_nil?
NoMethodError: undefined method `allow_nil?' for nil:NilClass
I want to ensure that the description
field is nil
not a blank string, but ensure that has_bar
is either true
or false
.
Excerpt from other question:
attributes.each do |column, value|
# invalid syntax
( self[column].present? && self[column].allow_nil? ) || self[column] = nil
end