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

Question

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

Answers 1
Added at 2016-12-23 22:12
Tags
Question

first of thank you all for taking the time reading this and answering.

i'm trying to find example or at least some help with the logic,

what i'm trying to do, is after a user removes his/her account to automatically transfer any resources to default admin account.

Using devise for user management.

Not sure what you might need to know more on this to be able to help out, But let me know and i shall provide more information.

Thank you in advance!

Update: (so in my user.rb model i have added this and the before_destroy)

It only returns me to 404 error..

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

Update 2: (fixed)

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
Answers to

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

nr: #1 dodano: 2016-12-23 22:12

You can do a before_destroy | before_update action (depending of your logic) on the user model, like this

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
Source Show
◀ Wstecz