Mike Cuddy Mike Cuddy - 5 months ago 10
Ruby Question

Working with many to many relationship in rails 4

I am currently working on a "practice" project with Rails 4 on many to many relationships. It is really one of the few things that I do not really know how to do with rails. Currently, I have two models-a physicians and a patients model. I have tied them together with an appointments model. Here is my code that I have so far: (I will only include the physicians and appoints code since I am not currently working with the patients model.)

Physicians model code:

class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, :through => :appointments
end


Appointments model Code:

class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
end


Physicians Controller:

class PhysiciansController < ApplicationController

def index
@physicians = Physician.all
end
end


Appointments Controller:

class AppointmentsController < ApplicationController

def index
@appointments = Appointment.all
@physicians = Physician.all
end
end


The Appointments index page code-where I want to show the doctors for each appointment:

<h1>Showing Appointments</h1>

<% @appointments.each do |appointment| %>

<% appointment.physicians.each do |physician| %>

<h3><%= appointment.physicians.name %></h3>

<% end %>

<% end %>


Schema:

create_table "appointments", force: :cascade do |t|
t.integer "physician_id"
t.integer "patient_id"
t.datetime "appointment_date"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "patients", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "physicians", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end


If needed my routes:

resources :patients
resources :physicians
resources :appointments


Yes, I do not have a lot of code. I am not using a scaffold and building everything myself. (It is how I feel I learn the best.) The specific error that I am getting is the following: undefined method
physicians' for #<Appointment:0x007fa7d5bf7370> as well as undefined method
each' for # if I change take away the s in the following line-since it will not match the physicians variable in the controller:

<% appointment.physician.each do |physician| %> (This shows each error message which I can easily solve.)

Now again, I am learning how to work with multiple databases. I can get the physician id to appear no problem. However, I want to go into the physicians db and pull out the name based on the physicians id. (Hopefully that makes sense.) Any help will be appreciated! Thank you for your help!

Answer

Since both Physician and Patient belong_to Appointment, it is a one-to-many association. Because of this, you need to refer to the association as singular:

appointment.physician # not physicians
appointment.patient # not patients

Your view should look like this:

<h1>Showing Appointments</h1>
<% @appointments.each do |appointment| %>
    <h3><%= appointment.physician.name %></h3>
<% end %>
Comments