的情況:如何加入多個表格?
表:
teacher :id :name
course :id :name
teachercourse :id :teacher_id :course_id
怎麼辦INNE用軌道加入這3張桌子?
編輯(我的模型):
class Course < ActiveRecord::Base
attr_accessible :name
has_many :teachercourses
has_many :teachers, through: :teachercourse
end
class Teacher < ActiveRecord::Base
attr_accessible :name
has_many :teachercourses
has_many :courses, through: :teachercourse
end
class Teachercourse < ActiveRecord::Base
attr_accessible :course_id, :teacher_id
belongs_to :course
belongs_to :teacher
end
EDIT2 - 我需要連接結果(show行爲):
class CourseController < ApplicationController
def show
#not real syntax
@course=Course.find(join:teacher,teachercourse,teacher :: where course='javacourse');
end
end
你期望從這個查詢得到什麼? – pkubicki 2012-08-15 08:13:08
希望得到:課程名稱,教師名稱 – Yosef 2012-08-15 08:14:13
除非您確實需要'Teachercourse',否則您可能會更好地使用['has_and_belongs_to_many'](http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_and_belongs_to_many)作爲一種模式。 – Chowlett 2012-08-15 08:17:40