用创造!何时启用accepts_nested_attributes

时间:2012-03-19 02:25:34

标签: ruby ruby-on-rails-3

我正在使用两种类似的方法来创建具有嵌套属性的记录。

第一种方式

m = Macrocycle.create!({"name"=>"sdfasdf", "description"=>"rt3t", 
                        "mesocycles_attributes"=>[{"name"=>"34fervaav", "_destroy"=>"false", "description"=>"asdfadf", "phase_number"=>"1", "workout_days_attributes"=>{"new_1332120641337"=>{"day"=>"1", "_destroy"=>"false"}}}]})
 => #<Macrocycle id: 37, name: "sdfasdf", description: "rt3t", created_at: "2012-03-19 01:57:05", updated_at: "2012-03-19 01:57:05"> 
 1.9.3p125 :047 > m.mesocycles[0]                                                                                                                                               
 => #<Mesocycle id: 30, name: "34fervaav", created_at: "2012-03-19 01:57:05", updated_at:  "2012-03-19 01:57:05", description: "asdfadf", macrocycle_id: 37, phase_number: 1>
1.9.3p125 :048 > m.mesocycles[0].workout_days
 => [#<WorkoutDay id: 2, day: 1, created_at: "2012-03-19 01:57:05", updated_at: "2012-03-19   01:57:05", mesocycle_id: 30>] 

如您所见,正在正确创建mesocycles和workout_days。但是,如果我使用这种方式(而不是使用数组用于child_attributes,使用具有每个子项的唯一键的哈希):

m = Macrocycle.create!({"name"=>"sdfasdf", "description"=>"rt3t",  "mesocycles_attributes"=>{"new_1332120631785"=>{"name"=>"34fervaav", "_destroy"=>"false", "description"=>"asdfadf", "phase_number"=>"1"}, "workout_days_attributes"=>{"new_1332120641337"=>{"day"=>"1", "_destroy"=>"false"}}}})
 => #<Macrocycle id: 38, name: "sdfasdf", description: "rt3t", created_at: "2012-03-19 02:15:39", updated_at: "2012-03-19 02:15:39"> 
1.9.3p125 :052 > m.mesocycles[0]
 => #<Mesocycle id: 31, name: "34fervaav", created_at: "2012-03-19 02:15:39", updated_at: "2012-03-19 02:15:39", description: "asdfadf", macrocycle_id: 38, phase_number: 1> 
 1.9.3p125 :051 > m.mesocycles[0].workout_days                             
 => [] 

未正确创建exercise_days。然而,正在正确地创造中周期。

无法弄清楚为什么会发生这种情况?我错过了傻事吗?我问的原因是我使用ryanb的nested_form gem在我的数据库中创建记录,而nested_form使用第二种方式。实际上,我只是在上面的控制台示例中直接使用了params哈希。

似乎使用带有唯一键的哈希为每个孩子工作,直到巢中的一个级别。

更新

关联代码:

Macrocycle Model:

class Macrocycle < ActiveRecord::Base
  #attr_accessible :name, :description, :mesocycles_attributes
  has_many :mesocycles, :dependent => :destroy  
  accepts_nested_attributes_for :mesocycles, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true
end

Mesocycle模型:

class Mesocycle < ActiveRecord::Base
  #attr_accessible :name, :phase_number, :description, :workout_days_attributes
  belongs_to :macrocycle
  #default_scope :order => 'mesocycles.phase_number ASC'
  has_many :workout_days, :dependent => :destroy
  accepts_nested_attributes_for :workout_days, :allow_destroy => true
end

锻炼日模型:

class WorkoutDay < ActiveRecord::Base
  attr_accessible :day, :blocks_attributes
  belongs_to :mesocycle
  has_many :blocks, :dependent => :destroy
  accepts_nested_attributes_for :blocks#, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true
end

1 个答案:

答案 0 :(得分:0)

在B上你的哈希在你的workoutday被包括之前就已经关闭了。

m = Macrocycle.create!({"name"=>"sdfasdf", "description"=>"rt3t",  "mesocycles_attributes"=>{"new_1332120631785"=>{"name"=>"34fervaav", "_destroy"=>"false", "description"=>"asdfadf", "phase_number"=>"1", "workout_days_attributes"=>{"new_1332120641337"=>{"day"=>"1", "_destroy"=>"false"}}}})

这有点神秘,很抱歉,但我认为这应该提供一些帮助