0

三种模式:创建错误在轨更新嵌套属性

class Order < ActiveRecord::Base 
    belongs_to :product 
    belongs_to :sale 
end 

class Sale < ActiveRecord::Base 
    has_many :orders 
    accepts_nested_attributes_for :orders, :reject_if => :all_blank 
end 

class Product < ActiveRecord::Base 
    belongs_to :greenhouse 
    has_many :orders 
end 

首先产品。然后,可以使用一个产品进行订单。那么,我期望的是,这个销售中充满了许多订单。但是,在保存Sale时,它只会忽略附加的Orders。

我只在Sale创建Order时找到了示例,或者父对象创建了Child对象,但在这种情况下,子对象或Order已经创建,并且只需要分配或关联与新的销售。

如何将子对象与新的父对象关联?

+0

你能告诉你哪里有错误吗? – dimakura

回答

0

只要确保你在你的SalesController这样的事情有许可证PARAMS:

params.require(:sale).permit(:each, :sale, :field, :as, :symbol, :and, orders_attributes: [:each, :order, :field, :as, :symbol]) 

这是最常见的问题,这给描述的行为。如果不是,我们需要更多的信息。