RoR:attr_encrypted 赋值未写入?
我在 RoR 3.0.5 中使用 attr_encrypted
(v 1.2.0) 来加密我不希望在数据库中以纯文本形式显示的凭据。当我更新加密字段时,它似乎没有保存到数据库中。
我的模型本质上是:(
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
请注意,“credentials=”方法的存在只是为了将 Rails 生成的 HashWithIn DifferentAccess 强制转换为普通哈希。它还为我提供了一个插入调试打印输出以验证我的数据的位置。)
但是当我尝试更新凭据时通过控制台,它不需要:
>> 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"}
为什么 s.credentials
没有更新为新值?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您只是在“credentials=”方法中设置一个名为“credentials_aux”的局部变量,尝试显式使用“self”
我实际上是该项目的维护者,所以如果上面的修复不起作用那么我'我会为此开一张新票。
I believe you're just setting a local variable called "credentials_aux" in your "credentials=" method, try explicitly using "self"
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.