Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.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 ActiveRecord验证不使用自定义属性分配方法_Ruby On Rails_Validation_Activerecord - Fatal编程技术网

Ruby on rails ActiveRecord验证不使用自定义属性分配方法

Ruby on rails ActiveRecord验证不使用自定义属性分配方法,ruby-on-rails,validation,activerecord,Ruby On Rails,Validation,Activerecord,我正在开发一个具有双因素身份验证的Rails应用程序。此应用程序中的用户型号有一个属性,双因素电话号码。在保存模型之前,我让模型验证此属性是否存在 为了确保电话号码以正确的格式保存,我创建了一个自定义属性分配方法,如下所示: def two_factor_phone_number=(num) num.gsub!(/\D/, '') if num.is_a?(String) self[:two_factor_phone_number] = num.to_i end class User

我正在开发一个具有双因素身份验证的Rails应用程序。此应用程序中的
用户
型号有一个属性,
双因素电话号码
。在保存模型之前,我让模型验证此属性是否存在

为了确保电话号码以正确的格式保存,我创建了一个自定义属性分配方法,如下所示:

def two_factor_phone_number=(num)
  num.gsub!(/\D/, '') if num.is_a?(String)
  self[:two_factor_phone_number] = num.to_i
end
class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable, :confirmable,
        :recoverable, :rememberable, :trackable, :validatable,
        :lockable

  attr_accessible :email, :password, :password_confirmation, :remember_me,
                  :first_name, :last_name, :two_factor_phone_number

  validates :first_name,              presence: true
  validates :last_name,               presence: true
  validates :two_factor_phone_number, presence: true

  # Removes all non-digit characters from a phone number and saves it
  #
  # num - the number to be saved
  #
  # Returns the digit-only phone number
    def two_factor_phone_number=(num)
      num.gsub!(/\D/, '') if num.is_a?(String)
      self[:two_factor_phone_number] = num.to_i
    end
end
我正在做一些验收测试,我发现如果这个方法在模型中,ActiveRecord验证将被忽略/跳过,并且可以创建一个新模型,而无需设置
two\u factor\u phone\u number

模型代码如下所示:

def two_factor_phone_number=(num)
  num.gsub!(/\D/, '') if num.is_a?(String)
  self[:two_factor_phone_number] = num.to_i
end
class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable, :confirmable,
        :recoverable, :rememberable, :trackable, :validatable,
        :lockable

  attr_accessible :email, :password, :password_confirmation, :remember_me,
                  :first_name, :last_name, :two_factor_phone_number

  validates :first_name,              presence: true
  validates :last_name,               presence: true
  validates :two_factor_phone_number, presence: true

  # Removes all non-digit characters from a phone number and saves it
  #
  # num - the number to be saved
  #
  # Returns the digit-only phone number
    def two_factor_phone_number=(num)
      num.gsub!(/\D/, '') if num.is_a?(String)
      self[:two_factor_phone_number] = num.to_i
    end
end
class用户
您可以添加格式验证:

validates :two_factor_phone_number, :format => { :with => /[0-9]/,
:message => "Only digits allowed" }
和/或创建另一个方法来设置此属性并在验证之前调用它

before_validation :update_phone_format

def update_phone_format
 ...
end

谢谢我希望有一个我在某处错过的设置,但我可以用一个before_验证过滤器。