Ruby on Rails Thursday, November 5, 2015


On Thu, Nov 5, 2015 at 3:41 PM, Colin Law <clanlaw@gmail.com> wrote:
> 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.

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


As shown in above code by using each loop in department I'm able to fetch department table's data in " <td><%= d.name %></td> ". Now I want to fetch same for consultant table i.e fetching consultant data in patient index file.

Rough example :

  <tbody>
    <% @departments.each do |d| %>
      <% d.patients.each do |patient| %>
        <tr>
          <td><%= patient.patient_name %></td>
          <td><%= d.name %></td>
            <% @consultants.each do |c| %>
              <td><%= c.name %></td>
            <% end %>
        </tr>
      <% end %>
    <% end %> 
  </tbody>

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

--
Cheers!

Deepak Kumar Sharma
Guru Nanak Dev Engineering College
India!

Blog: http://deekysharma.wordpress.com

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CALDHwN6Z7aeV2eBi5-zHzYN2HY07N2JnN4moEFjvbmQYh72Wuw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment