Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Good practice to always use ":dependent => :destroy?"

I recently came across an instance when I deleted a User from the database, but the index page for Posts broke because the User that wrote a specific post did not exist anymore.

This made me wonder whether it is good practice to always use :dependent => :destroy? Or is there a not-so-complicated alternative solution to not make the entire page break when the User is deleted? I guess it's more of a business decision, but I think I don't necessarily want to delete all the content when a User removes his account.

I suppose I could use something like

<%= link_to post.author.username unless post.author.blank? ...... %>

but that would make it a very tedious and messy task to have to include that in every line.

Any suggestions/tips on this matter?

like image 769
kibaekr Avatar asked Jan 16 '23 05:01

kibaekr


1 Answers

Rather then deleting the user deactivate the user, this way you won't break any of the relationships and your data will remain consistent.

like image 57
Himshwet Avatar answered Jan 29 '23 10:01

Himshwet