【发布时间】:2010-08-03 02:05:41
【问题描述】:
我有一个 Rails 模型 User,它有 name、email 和 hash 字段。
我通过以下方式保存数据:
@u = User.create(:name=>'test', :email=>"test@mail.com")
@u.save
如何合并before_create 回调,以便在保存记录之前,哈希值通过以下代码获取哈希字符串:
Digest::SHA1.hexdigest('something secret' + email)
我的User 模型会是什么样子?
class Employee < ActiveRecord::Base
before_create :set_hash
def set_hash
//what goes in here?
end
end
【问题讨论】:
-
顺便提一下,User.create 保存了用户,所以@u.save 是不必要的。如果您想在制作新模型和保存之间做点什么,请使用具有相同参数的 User.new。
标签: ruby-on-rails