【发布时间】:2015-09-27 20:52:05
【问题描述】:
三种型号:
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创建Order,或者父对象创建Child对象的例子,但是这种情况下,子对象,或者Order已经创建,只需要赋值或者关联新的销售。
如何将子对象与新的父对象关联起来?
【问题讨论】:
-
你能把出错的代码显示出来吗?
标签: ruby-on-rails ruby-on-rails-4 nested-attributes nested-form-for