I am practicing has_many through but I am stuck when the looping through the contents. It displays the whole object and not just the item want.
Models:
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
end
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, through: :appointments
end
class Patient < ActiveRecord::Base
has_many :appointments
has_many :physicians, through: :appointments
end
Controller:
class PatientsController < ApplicationController
def index
@patients = Patient.all
@appointments = Appointment.all
@physicians = Physician.all
end
def show
@patients = Physician.find(params[:id])
@appointments = Appointment.find(params[:id])
@physicians = Physician.find(params[:id])
end
end
patients/show.html.erb:
<%= @physicians.patients.each do |physician| %> <br><br>
<%= physician.name %>
<% end %>
After entering some random data to test I get the following result at http://localhost:3000/patients/1:
Jannie Runolfsdottir III
Eudora Moen [#<Patient id: 4, name: "Jannie Runolfsdottir III", phone: "356-388-3102 x3079", created_at: "2015-05-14 23:09:36", updated_at: "2015-05-14 23:09:36">, #<Patient id: 5, name: "Eudora Moen", phone: "(372) 713-5045 x3012", created_at: "2015-05-14 23:09:36", updated_at: "2015-05-14 23:09:36">]
I based my code from Views in a has_many :through relationship and http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association
The question is, how do I stop the whole object from displaying? I only want the names to display. It is behaving as I used the .inspect method.