title: Rails 4: count unread messages
body: <p>I'm trying to build a simple messaging system. 
  How do I display the number of total unread messages to every user in all conversations they are participating in? 
  Also, when the user goes to '/conversations' how do I recognize and label every conversation that has messages not read by the current_user?</p>
  
  <p>So far I have this:</p>
  
  <pre><code>class Message &lt; ActiveRecord::Base
    belongs_to :user
    belongs_to :conversation, :counter_cache =&gt; true
  end
  
  class Conversation &lt; ActiveRecord::Base
    belongs_to :user  
    has_many :messages
  
    belongs_to :recipient,
     :class_name =&gt; 'User',
     :primary_key =&gt; 'user_id',
     :foreign_key =&gt; 'recipient_id'
  end
  
  class User &lt; ActiveRecord::Base  
    has_many :conversations
    has_many :messages, :through =&gt; :conversations 
  end
  </code></pre>
  
  <p>schema</p>
  
  <pre><code>  create_table "conversations", force: true do |t|
      t.integer  "user_id"
      t.integer  "recipient_id"
      t.integer  "messages_count", default: 0
      t.datetime "created_at"
      t.datetime "updated_at"
    end
  
    create_table "messages", force: true do |t|
      t.integer  "user_id"
      t.integer  "conversation_id"
      t.text     "body"
      t.datetime "created_at"
      t.datetime "updated_at"
    end
  </code></pre>