ruby on rails - Get specific item from object -


i'm attempting return specific hospital name associated patient keep getting errors.

models: hospital has_many :patients  patients belong_to :hospital 

when rendering page call controller:

def list_patients       @patients = patient.all   end 

in view print out each patient , information:

<% @patients.each |patient| %> <table>     <tr>         <td><%= patient.first_name + "," + patient.last_name %></td>         <td><%= patient.ssn %></td>         <td><%= patient.dob %></td>         <td><%= patient.hospital.name%></td>     </tr>  </table> 

the above returns "undefined method name". if remove name can see object (<hospital:0x007fa1d9530138>) returned, i'm unable access specific attributes within object.

i can return specific hospital id, if like:

patient.hospital_id 

but stuck on how hospital name.

is code equal pasted one?

if so, belongs_to , not belong_to

if can't hospital.first.patients in console, make sure have hospital_id in patient model


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -