我有这个用户模型
class User < ActiveRecord::Base
attr_accessible :email, :name, :password_hash, :password_salt, :secret, :surname, :code, :updated_by
attr_accessible :password, :password_confirmation
attr_accessor :password
attr_protected :secret
before_save :encrypt_password
validates_presence_of :name
validates_presence_of :email
validates_uniqueness_of :email
validates_confirmation_of :password
validates_presence_of :password, :on => :create
def encrypt_password
if password.present?
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end
end
def self.generate_code
Array.new(32){Random.new.rand(36).to_s(36)}.join
end
... etc
end
并且在前面的控制器中我正在这样做
def code_generation
@user = User.find(:first, :conditions => {:email => params[:email]})
if @user
@user.code = User.generate_code
@user.save # HERE .SAVE IS RETURNING FALSE, SO I DONT HAVE IT IN DATABASE
end
end
问题是 @user.save 返回 false 并且数据库中的代码没有更改,我做错了什么?谢谢你。
编辑:这条线@user.code = User.generate_code
工作,我可以通过调试检查它。