I am trying to build a small database of Universities, Departments and
Courses. But for some reason I can’t seem to get a list of courses
attached to a Department.
So far I have the following Database:
=> Course(id: integer, course_name: string, length: integer,
description: text, programme: string, department_id: integer,
university_id: integer, created_at: datetime, updated_at: datetime)
=> Department(id: integer, department_name: string,
department_description: text, head_firstname: string, head_lastname:
string, head_email: string, university_id: integer, courses_id:
integer, created_at: datetime, updated_at: datetime)
=> University(id: integer, name: string, website: string, country:
string, city: string, departments_id: integer, courses_id: integer,
created_at: datetime, updated_at: datetime)
Models:
class Course < ActiveRecord::Base
belongs_to :department
belongs_to :university
end
class Department < ActiveRecord::Base
belongs_to :university
has_many :courses
end
class University < ActiveRecord::Base
has_many :departments
has_many :courses
end
For the Department I am trying to run the following controller
def index
@departments = Department.includes(:courses, :university)
and the following view:
<% @departments.each do |department| %>
that last line outputs #Course:0x1042b8270 and it fails when i try
to add something like department.courses.course_name
I am at a loss here and haven’t been able to find any clues on the
documentation as to what I might be doing wrong. If anyone could at
least point me in the right direction, that would be great.