简体   繁体   English

如何验证Rails中子模型中是否存在嵌套属性的父ID

[英]How to validate existence of parent id in child model in Rails for nested attributes

Here are 2 models.这里有2个模型。 Order takes nested attributes for order_items . Order采用order_items嵌套属性。

class order
  has_many order_items
  accept_nested_attributes_for :order_items
end

class order_item
  belongs_to :order
  validates :order_id, :presence => true  #this line cause spec failure. 
end

How to validates existence of order_id in order_item model?如何验证order_item模型中order_id存在? With nested_attributes, the existence of order_id in order_item is enforced already.使用nested_attributes, order_itemorder_id的存在已经被强制执行。 However when saving an order_item on its own (not along with order ), we still need to validate the order_id .但是,当order_item保存order_item时(而不是与order一起保存),我们仍然需要验证order_id

If I get it right you have trouble saving associated records with your setup:如果我做对了,您将无法保存与您的设置相关的记录:

params = {number: 'order-123', order_items_attributes:{product_id:1, quantity: 2, price: 3}}
Order.create params # => this should not work

To fix it, you need to tell Rails explicitly about associations, using inverse_of option:要修复它,您需要使用inverse_of选项明确地告诉 Rails 关联:

class Order
  # without inverse_of option validation on OrderItem
  # is run before this association is created
  has_many :order_items, inverse_of: :order
  accepts_nested_attributes_for :order_items
end

It's not required in your case but your can add inverse_of in OrderItem as well:在您的情况下不需要,但您也可以在OrderItem添加inverse_of

class OrderItem
   belongs_to :order, inverse_of: :order_items
   # ... the rest
end

More about using inverse_of option with associations can be read here .可以在此处阅读有关将inverse_of选项与关联一起使用的更多信息。

This:这个:

validates :order_id, :presence => true

will only make sure that some value for order_id was submitted.只会确保提交了order_id某些值。 You want this:你要这个:

validates :order, :presence => true

That will make sure that an order with the submitted order_id exists.这将确保存在提交的order_idorder

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

相关问题 Ruby on Rails - 嵌套属性:如何从子模型访问父模型 - Ruby on Rails - nested attributes: How do I access the parent model from child model Rails —如何使用子对象和强参数的嵌套属性填充父对象ID? - Rails — how to populate parent object id using nested attributes for child object and strong parameters? 嵌套表单-如何基于子模型的输入来验证父模型? - nested form - how to validate parent model based on inputs on child model? Rails:如何根据父母的属性验证模型? - Rails: How do I validate model based on parent's attributes? Rails嵌套表单:如何从子表单更新父属性 - Rails nested forms: How to update the parent attributes from a child form 访问父模型中的嵌套子属性? - Accessing nested child attributes in parent model? Rails - 验证嵌套属性唯一性与父级的范围父级 - Rails - Validate Nested Attributes Uniqueness with scope parent of parent 在创建子模型对象期间如何验证父模型属性? - How to validate a parent model attributes during creation of child model's object? Rails 4嵌套浅路由:如何在子控制器中获取父ID? - Rails 4 nested shallow routes: how to get parent id in child controller? rails验证嵌套属性 - rails validate nested attributes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM