我试图让我的一个模型接受嵌套属性。模型的名称是Tournament,Tournament有很多MapLists。另外值得注意的是,锦标赛验证至少存在一个MapList。同样,MapList验证是否存在tournament_id。作为参考,MapList只有3个属性:tournament_id,map_order和map_id。完全与锦标赛中出现的代码是:
accepts_nested_attributes_for :map_lists
我的问题是关于新的锦标赛记录。例如,我想使用以下代码创建一个新的锦标赛:
t = Tournament.new({map_lists_attributes: [{map_id: 1, map_order: 1}]}, as: :admin)
但是,此代码不起作用。如果我在实例化t后立即调用t.map_lists,我得到一个空关系。显然,我不能设置tournament_id因为我正在创建一个新的记录。我怎么解决这个问题?
答案 0 :(得分:0)
attr_assessible也可以防止嵌套属性...所以你需要像这样配置:
class Tournament
accepts_nested_attributes_for :map_lists
attr_assessible :map_lists_attributes #and all your others
end