Need guidance in fetching data using each

In my app I have 3 tables Patient, Department and Consultant. In
patient table I have passed foreign key of other other two tables.
Now, In my index view of patient I'm able to fetch data of department
table with each loop :

<tbody>
  <% @departments.each do |d| %>
    <% @patient = Patient.where(department_id: d.id) %>
      <% @patient.each do |patient| %>
        <tr>
          <td><%= patient.patient_name %></td>
          <td><%= patient.patient_age %></td>
          <td><%= d.name %></td>
    <% end %>
  <% end %>
</tbody>

Now my question how should I fetch data from consultant table using
each loop, same way as I did for department table in above code. I
need little guidance on this. Thanks in advance.

In my app I have 3 tables Patient, Department and Consultant. In
patient table I have passed foreign key of other other two tables.
Now, In my index view of patient I'm able to fetch data of department
table with each loop :

<tbody>
  <% @departments.each do |d| %>
    <% @patient = Patient.where(department_id: d.id) %>
      <% @patient.each do |patient| %>

You should not be using Patient.where here, rails will do this for you
if you setup the associations correctly. It should be something like
  <% @departments.each do |d| %>
    <% d.patients.each do |patient| %>
Such is the magic of Rails.

That assumes you have setup department has_many patients and patient
belongs_to department of course.

Now my question how should I fetch data from consultant table using
each loop, same way as I did for department table in above code. I
need little guidance on this. Thanks in advance.

You will need to give a little more information on what you want to
show for us to answer the question.

Colin

You should not be using Patient.where here, rails will do this for you
if you setup the associations correctly. It should be something like
<% @departments.each do |d| %>
<% d.patients.each do |patient| %>
Such is the magic of Rails.

+1

That assumes you have setup department has_many patients and patient
belongs_to department of course.

Wow! It works :). Learned something new. Thanks colin for this.

You will need to give a little more information on what you want to
show for us to answer the question.

<% @departments.each do |d| %> <% d.patients.each do |patient| %> <%= patient.patient_name %> <%= [d.name](http://d.name) %> <%= patient.consultant %> <% end %> <% end %>

As shown in above code by using each loop in department I’m able to fetch department table’s data in "

<%= d.name %> ". Now I want to fetch same for consultant table i.e fetching consultant data in patient index file.

Rough example :

<% @departments.each do |d| %> <% d.patients.each do |patient| %> <%= patient.patient_name %> <%= [d.name](http://d.name) %> <% @consultants.each do |c| %> <%= [c.name](http://c.name) %> <% end %> <% end %> <% end %>

Something like above which fetch name field from consultant table in patient’s index file according to their respective ID.

What is the relationship between consultants and patients, that is in
terms of has_many, belongs_to etc?

Colin

# Patient

# Patient

class Patient < ActiveRecord::Base
  belongs_to :department
  belongs_to :consultant
end

# Consultant

class Consultant < ActiveRecord::Base
  has_many :patients
end

# Department

class Department < ActiveRecord::Base
  has_many :patients
end

So there is only one consultant for a patient. Assuming that is the
information you want then something like

     <% @departments.each do |d| %>
       <% d.patients.each do |patient| %>
         <tr>
           <td><%= patient.patient_name %></td>
           <td><%= d.name %></td>
            <td><%= patient.consultant.name %></td>

I am a little surprised that this is not already clear to you. I
assume that you have worked right through railstutorial.org as I have
suggested to you several times previously. Have you done that? If
not then please do so, it will save you from getting stuck on simple
issues and having to waste both your time asking such questions and my
time answering them.

Cheers

Colin