帐户已删除将所有帖子从该帐户移至默认管理员帐户

Account deleted move all posts from that account to default admin account

首先感谢大家花时间阅读并回答。

我正在尝试寻找示例或至少对逻辑有一些帮助,

我想做的是在用户删除 his/her 帐户后自动将任何资源转移到默认管理员帐户。

使用设备进行用户管理。

不确定您可能需要了解更多信息才能提供帮助,但请告诉我,我会提供更多信息。

提前致谢!

更新:(所以在我的 user.rb 模型中我添加了这个和 before_destroy)

它只是 returns 我到 404 错误..

def transfert_associations_to_default_account
 #This user is the one ending account
 leavinguser = current_user.username

 #This will be the default user (archive) holder for all the content
 default_user = User.find_by(username: "archive")

 #default_user.resources << resources if default_user
leavinguser.posts.update(username: default_user)
leavinguser.reviews.update(username: default_user)
end

更新 2:(已修复)

def transfert_associations_to_default_account
 #This user is the one ending account
 leavinguser = self

 #This will be the default user (archive) holder for all the content
 default_user = User.find_by(username: "archive")

 default_user.posts << leavinguser.posts
 default_user.reviews << leavinguser.reviews
 end

你可以做一个before_destroy | before_update 对用户模型的操作(取决于您的逻辑),像这样

before_destroy :transfert_associations_to_default_account

def transfert_associations_to_default_account
 default_user = User.find()
 default_user.resources << resources if default_user
end