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 に答える