在上下文中包装多个验证

时间:2017-05-14 13:05:16

标签: ruby-on-rails validation ruby-on-rails-5

使用validation context我们可以:

validates :title, presence: true, on: :published
validates :content, length: { maximum: 50 }, on: :published

是否可以包含多个共享上下文的验证?

on: :published do
    validates :title, presence: true
    validates :content, length: { maximum: 50 }
end

1 个答案:

答案 0 :(得分:1)

是的,您可以使用with_options 方法对验证进行分组:

with_options(on: :published) do |record|
  record.validates :title, presence: true
  record.validates :content, length: { maximum: 50 }
end

有关详细信息,请参阅Rails Guidesthis articlesources