新手问题 Rails 如何在 model 里建立一个验证,使得旧数据忽略这个验证,而新数据不能忽略这个验证?

lengcb · June 05, 2017 · Last by lengcb replied at June 05, 2017 · 1530 hits

rails 如何在 model 里建立一个验证,使得旧数据忽略这个验证,而新数据不能忽略这个验证?

關鍵字 rails valid only create

Reply to hong_xin

是想旧数据更新时也忽略验证,但新数据更新时不忽略验证。我知道解决的方法了

埋坑中。

Reply to Rei

我想的太理想,是想旧数据更新时也忽略验证,但新数据更新时不忽略验证。我说的新旧数据区分是按照创建时间来看的,没有说明白

喔喔,理解錯誤了

class Order < ApplicationRecord
  validates :card_number, presence: true, if: :xxx?

  def xxx?

  end
end
validates : card_number, presence: true, if: proc { |c| c.id > 10000  }
Reply to CooperFu

是的,这是一个处理方法,谢谢

Reply to huobazi

嗯,明白

我觉得跑个脚本处理一下旧数据比较好吧😅

处理旧数据的时候不用 savesaveupdateupdate! 来更新数据就好

Reply to ACzero

哈哈,确实是一种处理方式

Reply to ywjno

嗯嗯,这问题我问得又歧义,其实关键是新旧数据的区分

You need to Sign in before reply, if you don't have an account, please Sign up first.