Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 保存前的MD5哈希值_Ruby On Rails_Ruby On Rails 3_Md5 - Fatal编程技术网

Ruby on rails 保存前的MD5哈希值

Ruby on rails 保存前的MD5哈希值,ruby-on-rails,ruby-on-rails-3,md5,Ruby On Rails,Ruby On Rails 3,Md5,我在订单表中有一个字段,该字段由其他值填充,其工作正常,如下例所示: company_name: A-Z Traders company_tel: (111) 111 1111 MD5_hash_field: A-Z Traders|(011) 111 1111 这是通过隐藏字段创建记录的工作 我需要的是,MD5_hash_字段在保存到数据库之前先进行MD5哈希处理 尝试添加以下内容: before_create :hash_field def hash_field self.md5_f

我在订单表中有一个字段,该字段由其他值填充,其工作正常,如下例所示:

company_name: A-Z Traders
company_tel: (111) 111 1111
MD5_hash_field: A-Z Traders|(011) 111 1111
这是通过隐藏字段创建记录的工作

我需要的是,MD5_hash_字段在保存到数据库之前先进行MD5哈希处理

尝试添加以下内容:

before_create :hash_field

def hash_field
  self.md5_field = Digest::MD5.hexdigest(self.md5_field)
end
但似乎什么也没做

编辑:

它在一定程度上起作用

<%= f.hidden_field :checksum, :value => '10011013800|Customer1|3299|ZAR|http://localhost|2012-01-30 18:30:00|secret' %> 

   Result should be: 31d1244f08a62f0551e9263c4835ba88

   but getting : aae1ee590e4d4e08e7c0363cca90a22b

你不能把代码改成

def hash_field
  p self.md5_field
  self.md5_field = Digest::MD5.hexdigest(self.md5_field)
end

看看它在日志中给你带来了什么?

你能添加一个示例记录来演示“之前”和“之后”状态吗?好吧,让它工作到一定程度是我做了一些愚蠢的事情,但我得到的结果被错误编辑了问题,知道结果不同的原因吗
def hash_field
  p self.md5_field
  self.md5_field = Digest::MD5.hexdigest(self.md5_field)
end