2016-04-20 106 views
0

问题:我只想在学生/演出中仅显示即将到来的参加者(不是过去的)。通过控制器导轨依赖于过滤器的数据

- scaffold lesson name starts_at:datetime ends_at:datetime

- scaffold attendance student:references lesson:references

- scaffold student name

student has_many :attendanceslesson has many :attendancesstudent has_many :lessons, through: :attendances

我试图

students_controller.rb:

@future_attendances = @student.attendances.where('lesson.starts_at > ?', Time.now)

学生/ show.html.haml:

 %table 
     %thead 
      %tr 
      %th Title 
      %th Starts at 
     %tbody 
      - @future_attendances.each do |attendance| 
      %tr 
       %td= attendance.lesson.name 
       %td= attendance.lesson.starts_at 

但它不起作用。有任何想法吗?

+0

您是否收到任何错误? – Uzbekjon

+0

什么“不起作用”呢?是否显示过去的参加人数,路线是否中断,是否显示错误等 – PhilVarg

+0

'SQLite3 :: SQLException:no such column:lesson.starts_at:SELECT“attendances”。* FROM“attendances”WHERE“attendances”。“student_id”=? AND(lesson.starts_at>'2016-04-20 22:07:13.208768')' –

回答

1

瞎猜,你能不能只是做:

@future_lessons = @student.lessons.where('starts_at > ?', Time.now) 

然后:

%tbody 
    - @future_lessons.each do |lesson| 
    %tr 
     %td= lesson.name 
     %td= lesson.starts_at 

,因为你正在寻找在课程表中的列您当前的代码不起作用。您需要使用joins方法将课程表添加到查询以使其工作。但是,我认为上面的路线就是你真正想要的。

+0

重要的一点是,它必须是'每个人都必须|出勤|' –

+0

是的,让我改写... – GoGoCarl

+0

@YaroAlexShm Doesn这似乎是出席(加入班)是必要的。只要跳到课堂上。 – GoGoCarl