title: Sort posts in order of latest commented post first
body: <p>I've following models</p>
  
  <p>Post:</p>
  
  <pre><code>has_many :comments
  </code></pre>
  
  <p>Comment:</p>
  
  <pre><code>belongs_to :post
  </code></pre>
  
  <p>and a controller called <code>pages</code> with following code in it.</p>
  
  <pre><code>def home
      @posts = Post.includes(:comments)
      respond_to do |format|
          format.html
      end
  end
  </code></pre>
  
  <p>So say if I've 3 posts P1, P2, P3; initially rendered in same order on <code>home</code> page. Later if P2 gets commented and I refresh the page the order should be P2, P1, P3. So whenever a post gets commented it should appear on top. All the posts should be sorted in descending order of time they get commented. How can I achieve this?</p>
  
  <p>My attempt - What I tried is very inefficient and definitely not a good practice. I get unique post_ids from comments in descending order and using the post_ids get the posts.</p>
  
  <p>I'm sure rails provides best way to do it. Help highly appreciated. Thanks.</p>