简体   繁体   English

一次性删除 Rails model 及其关联? 新手问题

[英]Delete Rails model and its associations in one action? NEWBIE question

I have a really simple association with the Devise user object where each user has one Profile (with more application specific stuff...) I am having no issues creating the User object and accessing the user and its profile object. I have a really simple association with the Devise user object where each user has one Profile (with more application specific stuff...) I am having no issues creating the User object and accessing the user and its profile object. ie, @user.profile即@user.profile

However, I'm having an issues when I try to delete the profile object - I'd assume that when I delete the User object, it would also delete each associated object.但是,当我尝试删除配置文件 object 时遇到问题 - 我假设当我删除用户 object 时,它也会删除每个关联的 object。 The association in my User object is like so accepts_nested_attributes_for:profile, :allow_destroy => true我的用户 object 中的关联就像这样接受_nested_attributes_for:profile, :allow_destroy => true

The has_one and belongs_to associations are set on both the User and Profile objects. has_one 和 belongs_to 关联在 User 和 Profile 对象上都设置了。 Maybe the issues is in Devise code - I'm stumped.也许问题出在 Devise 代码中——我很难过。 An idea what I'm missing here.一个想法我在这里错过了什么。

You need to specify :dependent on the association:您需要指定:dependent于关联:

has_one :profile, :dependent => :destroy

Look Association for more information.查找协会以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM