在我的情况下,通过链式尝试访问命名范围

时间:2019-02-20 15:11:58

标签: ruby-on-rails

我正在做旧的Rails项目。

一个学生可以学习许多课程。这是我的course类(在数据库中,有courses表):

class Course < ActiveRecord::Base
   ...
   named_scope :active, :conditions => ‘status is not null’
end

我想访问学生的第一门有效课程。我试图确保安全,因此请按照以下方式进行操作:

student.try(:courses).try(:active).try(:first).try(:name)

但是我得到了错误:

`undefined method` active' for []:Array`

那是为什么?

顺便说一句,我可以student.courses.active.first

0 个答案:

没有答案