的情况:如何加入多个表格?
表:
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