On 5 November 2015 at 09:39, Deepak Sharma <
deeky....@gmail.com> wrote:
> 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