我是 Rails 新手,并且有一个旧数据库,每个表中都包含 DateCreated 和 DateUpdated 列。我想在我的模型中使用内置的 created_at 和 updated_at 属性在保存之前设置这些字段,但我的尝试产生:NoMethodError (undefined method created_at' for #<Class:0x00000102b74d20>):
app/models/user.rb:45:in
set_create_update_time' app/models/user.rb:24:in block in <class:User>'
app/controllers/users_controller.rb:15:in
create'
型号代码:
class User < ActiveRecord::Base
attr_accessible :email, :user_name, :password, :password_confirmation, :DateCreated, :DateUpdated
alias_attribute :email, :Email
alias_attribute :user_name, :UserName
alias_attribute :remember_token, :RememberToken
self.table_name = "adm.User"
self.primary_key = "UserId"
has_secure_password
before_save { |user| user.email = email.downcase }
before_save { set_create_update_time}
before_save :create_remember_token
#validations
validates :user_name, presence: true, length: { maximum: 50 }
validates :password, presence: true, length: {minimum: 8}
validates :password_confirmation, presence: true
VALID_EMAIL_REGEX =
/\A[\w+\-.]+@[ a-z\ d\-.]+\.[a-z]+\z/i
# validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false}
private
def create_remember_token
# Create the token
self.remember_token = SecureRandom.urlsafe_base64
结尾
def set_create_update_time
self.DateCreated = created_at
self.DateUpdated = updated_at
end
end
任何帮助将不胜感激,谢谢!