简体   繁体   English

验证相关模型导轨

[英]Validation associated models rails

I have several model associations in one form. 我有几种形式的模型关联。 I would like to validate the presence of each nested attribute. 我想验证每个嵌套属性的存在。

I have the Product model, and for example I have this: 我有产品模型,例如,我有这个模型:

Product.rb 产品.rb

  has_many :ships, :dependent => :destroy
  has_many :sizes, :dependent => :destroy

  accepts_nested_attributes_for :ships
  attr_accessible :ships_attributes
  attr_accessible :size_tokens
  attr_reader :size_tokens

So I would like to validate the presence of :ships and :size_tokens when I send the action of my product's form. 因此,当我发送产品表单的操作时,我想验证:ships和:size_tokens的存在。 How can I validate the presence of those attributes? 如何验证这些属性的存在?

Thanks a lot. 非常感谢。

You can use validates_presence_of for both. 您可以同时使用validates_presence_of You can find a detailed explanation in here . 您可以在此处找到详细的说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM