0

我的模型有一个Recoverable模块CustomerCustomer模型使用has_secure方法进行认证。这是Customer模型:

class Customer < ActiveRecord::Base
  include Recoverable

  ##
  # Validations
  validates :email, format: { with: REGEX_EMAIL }, allow_nil: false, allow_blank: false
  validates_uniqueness_of :email
  validates_presence_of :email


  has_secure_password

  validates :password, length: { minimum: 6 }, if: :password_digest_changed?
  validates :password_confirmation, presence: true, if: :password_digest_changed?

end

这是Recoverable模块:

# encoding: utf-8

module Recoverable
  extend ActiveSupport::Concern

  def reset_password!(new_password, new_password_confirmation)
    self.password = new_password
    self.password_confirmation = new_password_confirmation
    if valid?
      self.reset_password_token = nil
      self.reset_password_sent_at = nil
    end
    save
  end
end

我的问题是在reset_password调用之后reset_password_tokenreset_password_sent_at不为空。它没有设置为空。更新查询未设置在列下方。为什么?我错过了什么吗?如果您需要更多信息,请告诉我。

我的环境:我正在使用 Rails 4 应用程序。

更新 1 当我放 self.inspect 时,我得到以下输出:

#<Customer id: 79, email: "milk@yahoo.com", password_digest: "$2a$10$U2knjpm5LF1V/sgXag0DcOpgZWHSpLw8nfCy4U8D57s6...", created_at: "2013-05-11 11:55:34", updated_at: "2013-05-16 10:04:45", reset_password_sent_at: nil, reset_password_token: nil>

更新 2:

  Parameters: {"utf8"=>"✓", "authenticity_token"=>"PbUhgSPvQZWXflT5fA1WhqhHJX3c7NMapg6eeDQvpBI=", "token"=>"fiMXi2_4cYCHsFMop9TJBL2Qeqc41xWhHA", "q"=>{"password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}
Unpermitted parameters: utf8, _method, authenticity_token, q
  Customer Load (0.4ms)  SELECT "customers".* FROM "customers" WHERE "customers"."reset_password_token" IS NULL LIMIT 1
Unpermitted parameters: password_confirmation
Unpermitted parameters: password
  Customer Exists (0.3ms)  SELECT 1 AS one FROM "customers" WHERE ("customers"."email" = 'milk@yahoo.com' AND "customers"."id" != 79) LIMIT 1
----------------------------BEFORE: 
#<ActiveModel::Errors:0xb593c280 @base=#<Customer id: 79, email: "milk@yahoo.com", password_digest: "$2a$10$/xYeks8yyaCMOFORFLMb1.xR7fxfskW6kHR4S2df/LTK...", store_id: 124, created_at: "2013-05-11 11:55:34", updated_at: "2013-05-16 11:56:52", reset_password_sent_at: nil, reset_password_token: nil>, @messages={}>
   (0.1ms)  BEGIN
  CACHE (0.0ms)  SELECT 1 AS one FROM "customers" WHERE ("customers"."email" = 'milk@yahoo.com' AND "customers"."id" != 79) LIMIT 1
  SQL (0.3ms)  UPDATE "customers" SET "password_digest" = $1, "updated_at" = $2 WHERE "customers"."id" = 79  [["password_digest", "$2a$10$/xYeks8yyaCMOFORFLMb1.xR7fxfskW6kHR4S2df/LTKUI001xu0O"], ["updated_at", Thu, 16 May 2013 19:58:25 ULAT +08:00]]
   (16.5ms)  COMMIT
---------------------------- SAVE: 
true
----------------------------AFTER: 
#<ActiveModel::Errors:0xb593c280 @base=#<Customer id: 79, email: "milk@yahoo.com", password_digest: "$2a$10$/xYeks8yyaCMOFORFLMb1.xR7fxfskW6kHR4S2df/LTK...", store_id: 124, created_at: "2013-05-11 11:55:34", updated_at: "2013-05-16 11:58:25", reset_password_sent_at: nil, reset_password_token: nil>, @messages={}>
4

2 回答 2

1

好的,最后如果您的模型在清除变量后无效,您可以这样做:

save(validate: false)

它将跳过验证并允许您保存无效模型

于 2013-05-16T12:04:09.503 回答
0

你能检查你的模型是否真的有效吗?我的意思是

if valid? 
  puts "valid"
  self.reset_password_token = nil
  self.reset_password_sent_at = nil
else
  puts self.errors.inspect
end

也许你有一些忘记的验证,你不会去那个街区?

于 2013-05-16T10:32:50.343 回答