Rails - 对加密字段求和
Rails - Summing an encrypted field
我在使用 Lockbox 之后有一个加密列 :amount
,我需要在我的控制器中求和。
目前,我有-
transaction_controller.rb
@one_month_transactions_sum = transactions.where(transaction_date: 1.month.ago..Date.today).sum(:amount).abs()
这给了我错误 - PG::UndefinedColumn: ERROR: column "amount" does not exist LINE 1: SELECT SUM(amount) FROM "transactions" WHERE "transactions"....
这是有道理的,因为我要求 rails 在我更改为密文的列上使用 PG SUM 函数。
我如何将控制器级别的事务与加密列相加?
如果您将数据作为不透明的二进制 blob 发送到数据库,您(自然地)失去了直接在数据库中操作这些字段的能力。看来您唯一的选择是将加密值提取到应用程序中,解密并在 ruby 中求和(慢慢地)。我还没有尝试过这个特定的库,但我猜这样的东西应该可以工作:
transactions.where(...).map(&:amount).sum
我在使用 Lockbox 之后有一个加密列 :amount
,我需要在我的控制器中求和。
目前,我有-
transaction_controller.rb
@one_month_transactions_sum = transactions.where(transaction_date: 1.month.ago..Date.today).sum(:amount).abs()
这给了我错误 - PG::UndefinedColumn: ERROR: column "amount" does not exist LINE 1: SELECT SUM(amount) FROM "transactions" WHERE "transactions"....
这是有道理的,因为我要求 rails 在我更改为密文的列上使用 PG SUM 函数。
我如何将控制器级别的事务与加密列相加?
如果您将数据作为不透明的二进制 blob 发送到数据库,您(自然地)失去了直接在数据库中操作这些字段的能力。看来您唯一的选择是将加密值提取到应用程序中,解密并在 ruby 中求和(慢慢地)。我还没有尝试过这个特定的库,但我猜这样的东西应该可以工作:
transactions.where(...).map(&:amount).sum