ruby-on-rails - postgres 9.4,pgcrypto 0.4.1,rails 4.2.0,ruby 2.2.0,引用预期的字符串,得到 Arel::Nodes::BindParam

标签 ruby-on-rails postgresql pgcrypto

尝试创建加密列时出现问题。 OS X 10.9.5

使用 gnupg 2.0.22 创建公钥/私钥:

    gpg -a --export > myKey .key.pub
    gpg -a --export-secret-key myKey > .key.prv

关于这里可能发生的事情的任何指示?我有一个带有 ssn 字段的模型 迁移添加为:

    add_column :users, :ssn, :binary

在用户模型中

    has_encrypted_column :ssn

为开发数据库添加了 pgcrpyto 扩展

在 config/initializers/pgcrypto.rb

    PGCrypto.keys[.public] = {path: '.key.pub'}
    PGCrypto.keys[.private] = {path: '.key.prv',armored: true, password: 'myPassword'}

rails c

    User.create(username:'x',ssn:'123-45-6789')

得到这个错误,column is ssn so it looks like it doing the right thing

TypeError: wrong argument type Arel::Nodes::BindParam (expected String)
from ... /vendor/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql/quoting.rb:19:in `escape_string'
from ... /vendor/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql/quoting.rb:19:in `quote_string'
from ... /vendor/ruby/2.2.0/gems/pgcrypto-0.4.1/lib/pgcrypto/adapter.rb:51:in `pgcrypto_encrypt_string'

下面的 **** 是我认为错误所在但不确定在这里调用什么方法的地方: 找不到很多关于 Arel::Nodes::BindParam 的文档。我假设这应该将值设置为“123-45-6789”,然后使用 pgcrypto_encrypt_string 调用对其进行加密。我验证了 key 是正确的。

     57     def pgcrypto_insert(arel)
     58       if table = PGCrypto[arel.ast.relation.name.to_s]
     59         arel.ast.columns.each_with_index do |column, i|
     60           if options = table[column.name.to_sym]
     61             key = options[:key] || PGCrypto.keys[:public]
     62             next unless key
     63             # Encrypt encryptable columns
     64   *****     value = arel.ast.values.expressions[i]
     65             arel.ast.values.expressions[i] = pgcrypto_encrypt_string(value, key)
     66           end
     67         end
     68      end
     69    end

非常感谢任何指点。弄清楚后很高兴 fork 并提出拉取请求。

最佳答案

猜测是由于 Rails 4.2.0 中的 Arel 6.0

在 rails 4.1.0、Arel 5.0 中

    Arel::Nodes::BindParams < Arel::Nodes::SqlLiteral < String < Object < BasicObject

在 rails 4.2.0 Arel 6.0 中

    Arel::Nodes::BindParams < Arel::Nodes::Node < Object < BasicObject

有关如何从 Arel::Nodes::Node 中提取值的任何提示

关于ruby-on-rails - postgres 9.4,pgcrypto 0.4.1,rails 4.2.0,ruby 2.2.0,引用预期的字符串,得到 Arel::Nodes::BindParam,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29102190/

相关文章:

ruby-on-rails - CentOS + Rails + nginx + Unicorn + MySQL + RVM(?) 方法?

ruby-on-rails - Rails 错误 "NoMethodError"- 我的第一个 ruby​​ 应用程序

ruby-on-rails - ActiveRecord/Ruby on Rails 中 update_all() 的返回值是多少?

postgresql - Terraform AWS RDS 实例跨区域复制 - US-west-1 中的只读副本

ruby-on-rails - 在 'lib/' 目录中实现自定义验证器是正确的方法吗?

postgresql - Postgres : ERROR: operator does not exist: character varying = bigint

postgresql - 将时间戳存储为 hstore 中的默认值

python - 索引错误 : tuple index out of range postgresql

c# - 解密由 pgcrypto 加密的数据(在 C# 中)

postgresql - 有没有办法在 Postgres 中验证签名的摘要?