title: If users have messages between them and a user deletes his account, the other user can no longer access the messages index in Rails
body: <p>I followed a tutorial to allow users to send each other messages on my app. I'm using devise for authentication. Basically if two or more users have conversations between them and one or more cancels their account, the remaining can no longer access the index page for conversations. I get <code>ActiveRecord::RecordNotFound</code> in <code>ConversationsController#index</code>.</p>
  
  <p>The code in bold is where the error was identified. What would be the best way to solve this?</p>
  
  <p>Thanks in advance</p>
  
  <p>Index view:</p>
  
  <pre><code>&lt;% @conversations.each do |conversation| %&gt;
    &lt;% if conversation.sender_id == current_user.id || conversation.recipient_id == current_user.id %&gt;
      &lt;% if conversation.sender_id == current_user.id %&gt;
        **&lt;% recipient = User.find(conversation.recipient_id) %&gt;**
      &lt;% else %&gt;
        &lt;% recipient = User.find(conversation.sender_id) %&gt;
      &lt;% end %&gt;
      &lt;tr&gt;
        &lt;td&gt;&lt;%= link_to (image_tag recipient.avatar.url, size: "50x50"), user_path(recipient) %&gt;&lt;/td&gt;
        &lt;td&gt;&lt;%= recipient.full_name %&gt;&lt;/td&gt;
        &lt;td&gt;&lt;%= link_to "View Message", conversation_messages_path(conversation) %&gt;&lt;/td&gt;
      &lt;/tr&gt;
    &lt;% end %&gt;
  &lt;% end%&gt;
  </code></pre>
  
  <p>The Controller: </p>
  
  <pre><code> class ConversationsController &lt; ApplicationController
     def index
       @users = User.all
       @conversations = Conversation.all
     end
  
    def create
     if Conversation.between(params[:sender_id],params[:recipient_id])
       .present?
        @conversation = Conversation.between(params[:sender_id],
         params[:recipient_id]).first
     else
      @conversation = Conversation.create!(conversation_params)
     end
     redirect_to conversation_messages_path(@conversation)
    end
  
    private
     def conversation_params
      params.permit(:sender_id, :recipient_id)
     end
    end
  </code></pre>
  
  <p>The Model:</p>
  
  <pre><code>class Conversation &lt; ActiveRecord::Base
    belongs_to :sender, :foreign_key =&gt; :sender_id, class_name: 'User'
    belongs_to :recipient, :foreign_key =&gt; :recipient_id, class_name: 'User'
  
    has_many :messages, dependent: :destroy
  
    validates_uniqueness_of :sender_id, :scope =&gt; :recipient_id
  
    scope :between, -&gt; (sender_id,recipient_id) do
      where("(conversations.sender_id = ? AND conversations.recipient_id =?) OR (conversations.sender_id = ? AND conversations.recipient_id =?)", sender_id,recipient_id, recipient_id, sender_id)
    end
  end
  </code></pre>