如何验证嵌套属性的数量最少?
两种模型:字段和值。使用accepts_nested_attributes_for 将值嵌套到Field
一个Field 可以有多个值。该字段具有属性 input_type,它是一个枚举,可以是 :text、:checkbox、:radio 或 :select。这些值是为此字段显示的选项,因此,如果 field.input_type 为单选或选择,则仅对该字段使用一个值是没有逻辑的。
使用字段验证,如何防止插入只有一个值的字段?
谢谢
Two models: Field and Values. Value is nested to Field using accepts_nested_attributes_for
A Field can have many Values. The field has the attribute input_type that is a enum and can be :text, :checkbox, :radio ou :select. The values are the options displayed for this Field, so, if field.input_type is radio or select, there is no logic in using just one value to the field.
Using validation on Field, how can I prevent the insertion of a Field with only one Value?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对你的问题不是 100% 清楚,但如果我理解正确的话,你想阻止 Field 模型的实例在只有一个关联值的情况下被保存吗?
据我所知,这将是最像 Rails 的方法,但您也可以将其编写为在
before_validation
回调中调用的模型方法。实际上有很多方法可以做到这一点。I'm not 100% clear on your question, but if I understand you correctly, you want to prevent an instance of the Field model from being saved if it has only one associated Value?
This would be the most Rails-ish way to do it as far as I know, but you could also write it as a model method that gets called in the
before_validation
callback. There's actually a number of ways to do this sort of thing.