Rails model.valid? flushing custom errors and falsely returning true

前端 未结 5 790
一向
一向 2021-01-07 16:56

I am trying to add a custom error to an instance of my User model, but when I call valid? it is wiping the custom errors and returning true.

[99] pry(main)&g         


        
5条回答
  •  感动是毒
    2021-01-07 17:04

    A clean way to achieve your needs is contexts, but if you want a quick fix, do:

    #in your model
    attr_accessor :with_foo_validation
    validate :foo_validation, if: :with_foo_validation
    
    def foo_validation
      #code 
    end
    
    #where you need it
    your_object.with_foo_validation = true
    your_object.valid?
    

提交回复
热议问题