【发布时间】:2015-08-14 09:12:30
【问题描述】:
我是 Rails 新手(通常是编码)。
我试图弄清楚如何在我的项目模型中编写一个范围,该范围基于两个属性来连接排序。
我的模特协会是:
projects has one sweep
sweep has one finalise
sweep belongs to projects
finalise belongs to sweep
在我的 project.rb 模型中,我尝试编写如下范围:
scope :currently_available_students, lambda { joins(@sweeps.finalise).group("projects.id").merge(sweep.finalise.finished) }
在我的 finalise.rb 中,我有一个名为 :finished_at 的日期时间属性。我也有:
scope :finished, -> { where(:draft => false) }
after_validation :set_publish_time
def set_publish_time
self.finalised_at = Time.now unless self.draft
end
我想找到已完成的项目。
在我的 projects_controller 中,我有:
has_scope :currently_available_students
def currently_available_students
@projects = Project.currently_available_students
render 'index'
end
当我尝试这个时,我收到一条错误消息:
undefined method `finalise' for nil:NilClass
谁能看到我做错了什么?
谢谢
【问题讨论】:
标签: ruby-on-rails lambda scope