1

paramsオブジェクトの更新または作成中に存在する場合にのみ、モデル内のいくつかの属性を検証する必要があります。

 validates :attribute_a,format: {with: /some_regex/},  :if => lambda{ |object| object.attribute_a.present? }

orである間は存在しない可能性attribute_aがあるものが複数あるように、それぞれのステートメントを記述する代わりに、複数の属性の存在を確認し、 or などの一般的な検証ですべてを検証できる方法はありますか。attributescreatedupdatedvalidatesinclusion_informat:{with:/some_regex/ }

明らかに間違っている次のコードのようなものが欲しかった。

validates :attribute_a,attribute_b,attribute_c,attribute_d,:format => {:with =>
 /some_regex/},  :if => lambda{ |object| object.attribute_name.present? }
4

1 に答える 1