I have a database which contains comments. In the model file I state that a comment
belongs_to :user
And in the database there is a user_id field associated with the user it belongs to. Now when a comment is deleted, rather than actually destroying the entry I want to remove the comment’s values and leave it as “This message has been deleted”. However, it seems I am unable to set the user_id field to nil through rails. I’ve tried
@comment.user_id = nil
@comment.save
As well as
@comment.user = nil
@comment.save
But neither work. What is the solution?
if you’re using a gem in creating a comment, there will be validations on the gem code. You can always bypass these validations by updating via sql. Either you bypass validations
@comment.save(validate: false)or update using sql@comment.update_column :user_id, nil.