因此,出于某些奇怪的原因,我不断收到此错误:
undefined method `>' for nil:NilClass
Rails.root: C:/Sites/booking_saloniser - Calendar
Application Trace | Framework Trace | Full Trace
app/models/user.rb:11:in `block in upcoming_appointments'
app/models/user.rb:11:in `upcoming_appointments'
app/controllers/appointments_controller.rb:8:in `index'
任命控制器
def index
@upcoming_appointments = current_user.upcoming_appointments
end
用户模型
def upcoming_appointments
appointments.order(appointment_time: :desc).select { |a| a.appointment_time > (DateTime.now) }
end
有机会有人可以帮助我解决此错误吗?
看来您的一个或多个约会记录为零appointment_time
。
如果不希望出现这种情况,则应在模型上添加一个不为null的验证,然后修复现有数据。
否则(这是快速解决方案),您可以将查询链接为不包含约会时间为零的记录:
def upcoming_appointments
appointments
.order(appointment_time: :desc)
.where.not(appointment_time: nil)
.select { |a| a.appointment_time > (DateTime.now) }
end
如果where.not
不可用(如果您使用的是旧版Rails),则也可以使用where("appointment_time != null")
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句