I have the following custom type:
class EncryptedTextType < ActiveRecord::Type::Text
def deserialize(encrypted_value)
return unless encrypted_value
Encryptor.decrypt(encrypted_value)
end
def serialize(plain_value)
return unless plain_value
Encryptor.encrypt(plain_value)
end
end
ActiveRecord::Type.register(:encrypted_text, EncryptedTextType)
this works fine but always leave my record dirty. every time I load a record from the database that uses this type, it gets dirty instantly.
This is the record:
class Organization < ApplicationRecord
attribute :access_key, :encrypted_text
[1] pry(main)> organization = Organization.last
Organization Load (0.7ms) SELECT "organizations".* FROM "organizations" ORDER BY "organizations"."created_at" DESC, "organizations"."id" DESC LIMIT $1 [["LIMIT", 1]]
=> #<Organization:0x00007fe000628198
id: "c968db2e-dd5a-4016-bf3d-d6037aff4d7b",
[2] pry(main)> organization.changed?
=> true
[3] pry(main)> organization.changes
=> {"access_key"=>["de07e...", "de07e..."]}
it is weird that even though the access key hasn't changed, AR still thinks it does.
CodePudding user response:
for future reference: I forgot to implement changed_in_place?
def changed_in_place?(raw_old_value, new_value)
raw_old_value != serialize(new_value)
end
this did the trick