Rails: good practice to always use ": depend =>: destroy?"
I recently met an instance when I deleted a user from the database, but the index page for the messages broke because the User who wrote the specific record no longer existed.
This made me wonder if it is always useful to use: depend =>: destroy? Or is there a not so complicated alternative solution, so as not to break the entire page when deleting the User? I assume that this is more of a business decision, but I think that I do not necessarily want to delete all the content when the User deletes his account.
I suppose I could use something like
<%= link_to post.author.username unless post.author.blank? ...... %> but that would make it very tedious and dirty to include this on every line.
Any suggestions / tips on this?
You must either use dependent: :destroy (or dependent: :delete ), or gently delete your users using the deleted_at column, and then browse the users to get those that are not deleted.
You can write the SoftDeletable module and include it in any model that you want to gently remove or use one of these stones: https://www.ruby-toolbox.com/categories/Active_Record_Soft_Delete .
I think there are several possibilities. But it depends on what you want.
I do not like websites that store my personal data and do not delete them when I want them to delete it.
You can set deleted_at attr to true if the user agrees to save the data. When the user does not agree with this, delete his profile and comments.
Alternatively, you can override the getter attribute for the username for the standard name for remote users:
# author.rb def username return read_attribute :username if deleted_at.blank? "unnamed" end