Rails ActiveRecord从迭代操作更新列值?

时间:2014-07-24 14:37:46

标签: ruby-on-rails ruby activerecord

我正在解密来自column_name的加密值,使其低于输出。

现在我想用我脚本输出中的解密值更新db column_name。我怎么做?

a=Model.pluck("column_name")

a.map { |i| Encryptor.decrypt(Base64.decode64(i), :key=>'bh2hbhbd43hbh3bh3b') }

输出:

 => ["49218123213167647472155545665651212", "46545649432177019560", "47511231231400014564554502209", "465944112312321312499558809", "4659445641231231231221669061537", "465914523564643217745645019560"]

谢谢

1 个答案:

答案 0 :(得分:1)

我假设您问如何使用解密值更新此列?如果是,只需迭代所有行:

Model.all.each do |row|
  decrypted = Encryptor.decrypt(Base64.decode64(row.column_name), :key=>'bh2hbhbd43hbh3bh3b')
  row.column_name = decrypted
  row.save!
end