考虑以下关联:
class Product < ActiveRecord::Base
belongs_to :shop
accepts_nested_attributes_for :shop
end
如果
params[:product][:shop_attributes] = {"name" => "My Shop"}
我这样做:
@product = Product.new(params[:product])
@product.save
@product
正如预期的那样,创建了一个名为“My Shop”的新商店并将其分配给。
但是,我无法弄清楚shop_attributes
包含 some时会发生什么id
,例如:
params[:product][:shop_attributes] = {"id" => "20", "name" => "My Shop"}
我收到以下错误:
Couldn't find Shop with ID=20 for Product with ID=
问题 1
这是什么意思?
问题2
如果是这种情况,即id
商店的商店是已知的,并且商店id
已经存在,我应该如何创建@product
该商店将被分配给它的商店?