<div class='comment_line'>
  <% current_comments = source_file.comments.where(:line_number => line_number).order(:created_at)
    current_comments.each do |comment|%>
    <div class='comment'>
      <div class='comment_box <%= (comment.user == current_user) ? 'this_user_' : 'other_user_'%>comment'>
        <%= simple_format(comment.message) %>
        <span class='who_commented'><%= "by #{comment.user.name} #{comment.user.last_name} #{time_ago_in_words(comment.updated_at)} ago" %></span>
      </div>
    </div>
  <% end %>
  <%= form_for(Comment.new(:line_number => line_number, :source_file => source_file)) do |f| %>
    <%= f.hidden_field :source_file_id %>
    <%= f.hidden_field :line_number %>
    <div class='comment comment_textarea'>
      <%= f.text_area :message %> <%= f.submit '+' %>
    </div>
    <a class="comment comment_toggle"><%= if current_comments.empty? then 'Add Comment' else 'Reply' end%></a>
  <% end %>
</div>
<div class='comment_line'>
  <% current_comments = source_file.comments.select{ |c| c.line_number == line_number }
    current_comments.each do |comment|%>
    <div class='comment'>
      <div class='comment_box <%= (comment.user == current_user) ? 'this_user_' : 'other_user_'%>comment'>
        <%= simple_format(comment.message) %>
        <span class='who_commented'><%= "by #{comment.user.name} #{comment.user.last_name} #{time_ago_in_words(comment.updated_at)} ago" %></span>
      </div>
    </div>
  <% end %>
  <%= form_for(Comment.new(:line_number => line_number, :source_file => source_file)) do |f| %>
    <%= f.hidden_field :source_file_id %>
    <%= f.hidden_field :line_number %>
    <div class='comment comment_textarea'>
      <%= f.text_area :message %> <%= f.submit '+' %>
    </div>
    <a class="comment comment_toggle"><%= if current_comments.empty? then 'Add Comment' else 'Reply' end%></a>
  <% end %>
</div>
Improve performance for comment loading
Improve performance for comment loading

The “where” statement was hitting the database a lot which was
degrading performance. Using a lambda expression “select” to work with
the data in memory improves performance dramatically.
