0

私は3つのモデルを持っています:

学生-クラス-評価

生徒をクラスに関連付けると、生徒はその特定のクラスの1つの評価に自動的に関連付けられる必要があります。

クラスにはたくさんの生徒がいます。学生は多くのクラスに属しています学生は多くの評価を持っています評価は1人の学生と1つのクラスに属しています。

学生のURLを開くと、学生を採点できるように、そこに表示される評価が必要です。

これどうやってするの?

これが私のroutes.rbです

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

私は学生/1/教室/1/評価を得ることができます しかし私の学生/ショーページで私はこれを試します:

<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')
4

1 に答える 1

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

ところで:「クラス」モデルの別の名前を見つける必要があるかもしれません。それはいくつかの混乱につながる可能性があります;)

更新: 上記のコードは、データベース構造に少なくとも次の列があることを前提としています。

+-------------------------+
|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                |
|...                           |
+------------------------------+
于 2013-03-13T19:37:25.397 に答える