3

当我向 singleton_class 添加验证时,它似乎被分配给了基类,并且它不会触发任何一个。

class Example
  attr_accessor :title, :some_boolean
  include ActiveModel::Validations
end


puts Example.validators  # []

with_validations = Example.new
with_validations.singleton_class.send :validates, :title, :presence => true

puts with_validations.valid?  #true

puts Example.validators.length # 1

我想发生的事情:

with_validations.valid?  # false
Example.new.valid?       # true
4

1 回答 1

0

您可以使用“on”选项进行验证。

validates :title, presence: true, on: :draft

example = Example.new
example.valid? # true
example.valid?(:draft) #false
于 2013-11-04T07:17:57.233 回答