Rails 3 嵌套表单带有 has_many :through,连接表中的条目在更新后不会被删除
我有一个“用户”模型,它通过连接表“user_number”模型与“Number”模型具有 has_many 关系。 使用:
accepts_nested_attributes_for :numbers, :allow_destroy => true
我在“用户”模型中 一切正常,除了每当我在编辑表单中删除用户的号码时, 关联号码在“number”表中被正确删除,但“user_number”连接表中的条目未被正确删除。
在更新控制器操作中,我仅使用此操作:
...
if @user.update_attributes(params[:user])
...
How can Iforcerailsto也删除连接表中的关联条目?
I have a 'User' model which has a has_many relationship to a 'Number' model through a join table 'user_number' model.
I use:
accepts_nested_attributes_for :numbers, :allow_destroy => true
in the 'User' model. Everything works fine except that whenever I delete a number from a user in the edit form,
the associated number is deleted correctly in the 'number' table, but not the entry in the 'user_number' join table.
In the update controller action I only use this:
...
if @user.update_attributes(params[:user])
...
How can I force rails to also delete the associated entry in the join table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要让 User Accepts_nested_attributes_for :join_class, :allow_destroy =>真的。然后,您删除该关联。 has_many :through 背后的想法是,除非没有其他关联,否则不会删除 n->m 映射的 m 部分。
You need to have User accepts_nested_attributes_for :join_class, :allow_destroy => true. Then, you delete the association. The idea behind a has_many :through is that you don't delete the m part of the n->m mapping unless there are no other associations left..