说我有两个表,trees
和和一个连接表tree_apples
。假设tree_apples
表有几个重要列,分别叫做rotten
(boolean)和branch
(int)。Rails update_nested_attributes。如果存在,HOw会进行更新,如果不存在则创建?
说,在控制器trees
,我有这样的:
@tree.update_nested_attributes(tree_params)
和我tree_params
方法是这样的:
tree_apple_params: {
:apple_id,
:rotten,
:branch
}
说,我已经更新有tree_apple与ID树3即rotten: true
和branches: nil
和传入参数到此控制器更新分支具有这样的参数:
tree_apple_params: [{
apple_id: 3,
rotten: nil,
branch: 5
},
{
apple_id: 4,
rotten: nil,
branch: 6
},
...
]
所以这是params来创建或更新一堆tree_apples
。参数来自的页面想要更新tree_apple
的分支,该分支引用3的apple_id
,并且对于此tree_apple
,它不提供任何关于腐烂状态的输入。我不想创建另一个tree_apple
,它引用apple_id: 3
对于rotten_state具有nil
。我想更新现有的tree_apple
。所以,对于这个tree_apple
,我想找到它,然后更新它与分支5.但对于参考苹果,不存在此树上的所有其他参数,我想创建新tree_apples
如果tree_apple
不存在。
有没有办法做到这一点?这个逻辑是否属于控制器或模型中的某种回调?
@jwan没有能够解决问题了吗? – George