title: How to tell if something has been read in Rails for notification
body: <p>I'd like to implement a simple notification system on my site where an unseen/unread item is displayed to the user. Similar to the one used across Stack Exchange for the user's inbox where unread comments on questions, etc are displayed. </p>
  
  <p>I came across <a href="https://stackoverflow.com/questions/1561199/keep-track-of-read-unread-user-messages-with-a-messageclicks-table">this question</a> that provides an overview of how I'd do this. What I'm confused about is how to figure out if something has been read. I could add a <code>read_at</code> column but how do I actually fill it? If anyone could help me with some basic guidance I'd appreciate it!</p>
  
  <p><strong>UPDATE #1:</strong> What if I add a conditional to my Item#show action where I check the <code>user_id</code> (ID of the user creating the item) against <code>current_user.id</code>. Something like the below:</p>
  
  <pre><code>unless @item.user_id == current_user.id
    @item.read_at = Time.now
  end
  </code></pre>
  
  <p><strong>UPDATE #2:</strong> Using the code below, I'm attempting to update the message's <code>read_at</code> if its <code>recipient_id</code> matches the <code>current_user</code> ID. However it's not working.</p>
  
  <pre><code>def show
    @message = Message.find(params[:id])
    if @message.recipient_id == current_user.id
      @message.read_at == Time.now
      @message.save
    end
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml =&gt; @message }
    end
  end
  </code></pre>
  
  <p><strong>FINAL UPDATE:</strong> Thanks to @prasvin, here's my solution. I added a <code>read_at</code> column to the object. The object also has an existing <code>recipient_id</code> column. So in my Controller's show action, I put the following:</p>
  
  <pre><code>def show
    @message = Message.find(params[:id])
    if @message.recipient_id == current_user.id
      @message.update_attributes(:read_at =&gt; Time.now)
    end
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml =&gt; @message }
    end
  end
  </code></pre>
  
  <p>Then in my helper.rb file:</p>
  
  <pre><code>def new_messages
    Message.where(:recipient_id =&gt; current_user.id, :read_at =&gt; nil).count
  end
  </code></pre>
  
  <p>And in my layout:</p>
  
  <pre><code>&lt;% if new_messages &gt; 0 %&gt;&lt;span class="notification"&gt;&lt;%= new_messages %&gt;&lt;/span&gt;&lt;% end %&gt;
  </code></pre>