2013-03-13 25 views
0

紅寶石我有3種型號:全自動聯想兩款車型在軌道上

學生 - 類 - Avaliation

我需要的是,當我一個學生與類關聯,學生altomatcly關聯對該特定班級進行一次評估。

一個班有很多學生。 該學生屬於多個班級 學生有很多評語 評語屬於一個學生和一個班級。

當我打開學生的網址時,我需要評估以評估學生。

我該怎麼做?

好了,所以這是我的routes.rb

resources :students do 
    resources :classrooms do 
    resources :avaliations 
    end 
end 

我能得到學生/ 1 /教室/ 1/avaliation 但在我學生/顯示頁我試試這個:

<p> 
    <b>Name:</b> 
    <%= @student.name %> 
</p> 

<h3>Class</h3> 
<% @student.classrooms.each do |classroom| %> 
    <%= classroom.name %> 
<% end %> 

但我得到這個錯誤:

SQLite3::SQLException: no such column: classrooms.graded: SELECT "classrooms".* FROM "classrooms" INNER JOIN "avaliations" ON "classrooms"."id" = "avaliations"."classroom_id" WHERE "avaliations"."student_id" = 1 AND ("classrooms"."graded" = 't') 

回答

0

未經測試:

class Avaliation 
    belongs_to :student 
    belongs_to :class 
end 

class Class 
    has_many :avaliations 
    has_many :students, :through => :avaliations, :conditions => {:graded => true} 
end 

class Student 
    has_many :avaliations 
    has_many :classes, :through => :avaliations, :conditions => {:graded => true} 
end 

BTW:也許你應該找到適合您的 「類」 - 模型的另一個名稱。這可能會導致一些混亂;)

更新: 上面的代碼假定您有至少你的數據庫結構以下列:

+-------------------------+ 
|Student (table students) | 
|id: integer    | 
|....      | 
+-------------------------+ 

+------------------------------+ 
|ClassRoom (table class_rooms) | 
|id: integer     | 
|name:string     | 
|....       | 
+------------------------------+ 

+------------------------------+ 
|Avaliation (table avaliations)| 
|student_id:integer   | 
|class_room_id:integer   | 
|graded:boolean    | 
|...       | 
+------------------------------+ 
+0

是什麼:磨碎suposed是?我不熟悉rails和編程邏輯。我更新了這個問題,你能幫忙嗎? – Allan 2013-03-15 21:32:50

+0

這是什麼邏輯? – Allan 2013-03-15 21:53:39

+0

在上面的代碼段「分級」被認爲是表的avaliations列。我和你的表怎麼可能看起來像一個例子更新了答案。 – jack 2013-03-19 05:36:13