Reputation: 35159
I'm using attr_encrypted
(v 1.2.0) in RoR 3.0.5 to encrypt credentials that I don't want appearing as plain text in my db. When I update the encrypted field, it appears that it's not getting saved into the db.
My model is essentially:
class Service << ActiveRecord::Base
attr_encrypted :credentials_aux, :key => KEY, :attribute => 'encrypted_credentials', :encode => true, :marshal => true
def credentials
credentials_aux
end
def credentials=(c)
h = {}.update(c) # coerce HashWithIndifferentAccess to a vanilla hash
credentials_aux = h
end
...
end
(Note that the 'credentials=' method exists simply to coerce a Rails-generated HashWithIndifferentAccess into a vanilla hash. It also gives me a place to interpose debugging printout to verify my data.)
But when I try updating credentials via the console, it doesn't take:
>> s = Service.find(19)
=> #<Service id: 19, encrypted_credentials: "10VfHU7IkdrFb4Q6Hj18YtY81rbRp3sIuoVUl8CHNj88cq1XFo2...",>
>> s.credentials
=> {"user_id"=>"fred.flintstone", "password"=>"supersecret"}
>> s.credentials = {"user_id" => "barney.rubble", "password" => "notsosecret"}
=> {"user_id" => "barney.rubble", "password" => "notsosecret"}
>> s.credentials
=> {"user_id"=>"fred.flintstone", "password"=>"supersecret"}
Why didn't s.credentials
get updated to the new value?
Upvotes: 2
Views: 540
Reputation: 640
I believe you're just setting a local variable called "credentials_aux" in your "credentials=" method, try explicitly using "self"
def credentials=(c)
h = {}.update(c) # coerce HashWithIndifferentAccess to a vanilla hash
self.credentials_aux = h
end
I'm actually the maintainer of that project, so if the fix above doesn't work then I'll open up a new ticket for this.
Upvotes: 3