-
Notifications
You must be signed in to change notification settings - Fork 22k
Closed
Description
Okay, so my Google foo is failing me, apologies if this has been answered before and I just can't find it.
I'm wanting to strip null bytes from all text columns because Postgres cannot accept null bytes, and my application doesn't care about storing them.
One approach that you can take is to override each attribute setter individually:
class User < ApplicationRecord
def name=(value)
if value.is_a?(String) && value.present?
super(value.delete("\u0000"))
else
super(value)
end
end
endThis works, but it quickly gets unwieldy if you need to do this for every string attribute in the application.
Ideally, I would be able to override it globally, e.g. something like:
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
def attribute=(value)
if value.is_a?(String) && value.present?
super(value.delete("\u0000"))
else
super(value)
end
end
endSo my questions are:
- Is wanting to do this sane? How do others approach users providing null bytes when you're using Postgres?
- Are there any ways to override the attribute setters for all setters in an application? I would be fine with monkey patching / meta programming if that's the only option available.
Metadata
Metadata
Assignees
Labels
No labels