On 5 November 2015 at 09:39, Deepak Sharma <deeky.sharma@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
--
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/CAL%3D0gLtb2g8bwZe6xpvy5AZn8Hr4UAtj6AP6j5bnzQr%2B0m0%3D2g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment