0

我有这个挑战,希望获得与特定部门 ID 匹配并匹配特定课程年份的所有课程。前者已实现,但在实现后者时遇到问题。

#course.rb
has_and_belongs_to_many :departments

#department.rb
has_and_belongs_to_many :courses

[编辑]

#student.rb
belongs_to :department
has_many :courses

到目前为止有这个

## Fetch Courses that Match that studentdept
@coursedept = Course.all(:include => :departments , :conditions => ["departments.id = ? and Courses.year = ? " , studentdept,courseyear]

以上返回一个空的结果集。没有课程标准,它可以工作。

我如何最适合那里的课程标准。尝试了各种变体

4

1 回答 1

0

Rails 内置了这个功能。假设你的 has_and_belongs_to_many 连接表设置正确,你可以获得这样的部门课程:

courses = studentdept.courses

和特定年份的课程如下:

courses = studentdept.courses.where(:year => courseyear)
于 2013-03-26T10:39:56.960 回答