以下是2个模型。 Order
需要order_items
的嵌套属性。如何在子模型中验证Rails嵌套属性中parent id的存在
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
如何验证的order_id
存在于order_item
模式?使用nested_attributes,order_item
中order_id
的存在已被执行。但是,当自行保存order_item
(不是与order
一起)时,我们仍然需要验证order_id
。
你究竟想达到什么目的?你能向我们展示导致你的问题的创建/新电话吗? – panmari