使用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
答案 0 :(得分:1)
是的,您可以使用with_options
方法对验证进行分组:
with_options(on: :published) do |record|
record.validates :title, presence: true
record.validates :content, length: { maximum: 50 }
end
有关详细信息,请参阅Rails Guides,this article和sources。