我正在做旧的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