0

I've got it so that I have a list of the unique patients a clinician is having a conversation with. I can call up details about the patients but I can't call up anything to do with the associated comment.

app\views\comments\index.html.erb

    <% @comments.map(&:patient).uniq.each_with_index do |comment, index| %>
      <div class="profile-post color-one">
          <span class="profile-post-numb"><%= index+1 %></span>
          <div class="profile-post-in">
              <h3 class="heading-xs"><a><%= link_to "#{comment.first_name} #{comment.last_name}", comment_path(comment) %></a></h3>
              <p><%= comment.diagnosis %><i class="pull-right"><%= link_to "Edit", edit_comment_path(comment) %> <%= link_to "Delete", comment_path(comment), method: :delete, data: {confirm: 'Are you sure you want to delete'} %></i></p>
          </div>
      </div>
    <% end %>

The link_to are also broken since they are supposed to direct to a comment page but are passing patient.id instead of comment.id.

comments_conrtoller.rb

def index
 @comments = current_clinician.comments
end

comment.rb

class Comment < ActiveRecord::Base
 belongs_to :clinician
 belongs_to :patient
end
Skiapex
  • 153
  • 3
  • 14

1 Answers1

0

@comments.map(&:patient) gives objects which are of class Patient, and that is why the links are passing patient_id instead of comment_id.

You should be using group instead of map here.

Something along the lines of @comments.group(:patient).. ; the loop syntax has to be changed to work with the output from this scope.

See http://apidock.com/rails/v4.0.2/ActiveRecord/QueryMethods/group for more details about how group works.

Prakash Murthy
  • 12,923
  • 3
  • 46
  • 74
  • I'm still getting: SQLite3::SQLException: no such column: patient: SELECT "comments".* FROM "comments" GROUP BY patient. I've changed the view to: `code`<% @comments.group(:patient).each_with_index do |comment, index| %>`code` and the controller to: `code`def index @comments = Comment.all end`code` For now until I get it working. I think I can use 'having' to get it to just show ones from the current user – Skiapex Mar 16 '15 at 13:21
  • `@comments.group(:patient_id)...` . I would suggest playing with this within the console before figuring out the correct looping construct to use. – Prakash Murthy Mar 16 '15 at 13:23
  • The best way ended up being: '<% @comments.group(:patient_id).each_with_index do |comment, index| %>' Thanks for your help! – Skiapex Mar 16 '15 at 15:11
  • Hi again Prakash - your suggestion worked great locally but gave my a GroupingError when I pushed to heroku. **PG::GroupingError: ERROR: column "comments.id" must appear in the GROUP BY clause or be used in an aggregate function** If you have any advice for this it would be much appreciated – Skiapex Apr 15 '15 at 03:02