I want to override the default rails string type to always strip inputs. I’m aware there are multiple gems which solve this problem, but I felt a custom type is cleaner (while these gems rely on callbacks).
Following documentation I put the following in an initializer:
class StrippedStringType < ActiveRecord::Type::String def cast(value) super&.strip end end ActiveRecord::Type.register(:string, StrippedStringType, adapter: :postgresql)
StrippedStringType, however my models still use
ActiveModel::Type::String (which I could inspect by calling eg.
What’s also a bit weird: as per documentation I expected
ActiveRecord::Type.register(:string, ...) to raise an error when called without
:override, as we are overriding a native type, but no such thing happened.
Does anyone have any experience with this kind of thing?