Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails,与父对象一起创建关联记录?_Ruby On Rails_Nested Attributes - Fatal编程技术网

Ruby on rails Rails,与父对象一起创建关联记录?

Ruby on rails Rails,与父对象一起创建关联记录?,ruby-on-rails,nested-attributes,Ruby On Rails,Nested Attributes,也许我不知道如何询问/搜索这个特定的东西,但基本上我想在创建父对象时创建一些关联的模型。。。假设我有以下情况: 我有一个配方,其中有许多成分模型。。。有没有一种方法可以让它们同时完成,比如说这是我种子任务的一部分: Recipe.create({ :title => 'apple pie', :description => 'just apple pie', :ingredients => { [0] => {:title => 'apples'

也许我不知道如何询问/搜索这个特定的东西,但基本上我想在创建父对象时创建一些关联的模型。。。假设我有以下情况:

我有一个
配方
,其中
有许多
成分
模型。。。有没有一种方法可以让它们同时完成,比如说这是我种子任务的一部分:

Recipe.create({
  :title => 'apple pie',
  :description => 'just apple pie',
  :ingredients => {
    [0] => {:title => 'apples'},
    [1] => {:title => 'sugar'},
    [2] => {:title => 'pie crust'}
  }
})
还是说我完全疯了?必须有某种类似的方法来做到这一点,不创建父模型,然后所有的子模型。。。等等,等等,非常接近。看


请注意,您需要将“接受:配料的嵌套属性”添加到配方模型中。

您还需要将其添加到配方模型中

attr_accessible :ingredients_attributes
attr_accessible :ingredients_attributes