ActiveModel:验证相等性的值

sch*_*jos 2 validation ruby-on-rails activemodel

我有一个ActiveModel类,其实例仅在被触摸时才有效。以下代码有效:

class Base
  include ActiveModel::Model
  validates :touched?, inclusion: { in: [true] }

  def update(params = {})
    initialize(params)
    @touched = true
  end

  def touched?
    !!@touched
  end
end
Run Code Online (Sandbox Code Playgroud)

但我不喜欢它。这不太好。我想写这样的东西:

validates: touched?, equality: true
Run Code Online (Sandbox Code Playgroud)

有没有更好/更短的编写方式而不使用自定义验证器?如果它也适用于booleans以外的值,那就太好了。

小智 5

validates :touched?,  acceptance: true
Run Code Online (Sandbox Code Playgroud)