ruby-on-rails - 如何自动销毁accepted_nested_attributes_for的关联
问题描述
我有User
多个Posts
class User
has_many :posts
accepts_nested_attributes_for :posts, allow_destroy: true
end
class Post
belongs_to :user
end
在控制器中,我将通过更新
@user.update({ posts_attributes: [{uuid:'blah1'},{uuid:'blah2'}] })
posts
我希望它可以在每次更新中自动销毁不存在的东西。
@user.update({ posts_attributes: [{uuid:'blah1'},{uuid:'blah2'}] }) # create blah1, create blah2
@user.update({ posts_attributes: [{uuid:'blah2'},{uuid:'blah3'}] }) # delete blah1, create blah3
我怎样才能做到这一点?
更新:
我知道这可以通过
@user.update({ posts_attributes: [{uuid:'blah1',:_destroy:true},{uuid:'blah2'},{uuid:'blah3'}] })
但从我的角度来看,这并不体面。它可以是内部模型而不是外部模型吗?
更新:
我找到了这个链接,但它看起来很复杂。
解决方案
推荐阅读
- unreal-engine4 - Unreal 4.25 - 不能在基于“对象”类的蓝图中使用“GetAllActorsOfClass”
- python - 根据其他数据框列值过滤熊猫数据框
- java - 如何在 spring-cloud-stream-binder-kinesis 中使用 STSAssumeRoleSessionCredentialsProvider 进行配置
- winforms - 在winforms用户控件中托管一个对话框?
- python - 使用过滤器功能和 for 循环过滤多个数据帧
- node.js - Discord.js 机器人有没有办法检查提及?
- r - 结合边属性生成顶点属性
- azure - 从 Plesk 域控制器到 Azure 的 DNS 传输
- python - 如何消除 B210 USRP 传输的失真?
- node.js - Firebase nodejs admin sdk - 从 Firebase 存储中删除文件会导致套接字挂起错误