【发布时间】:2023-04-04 16:12:01
【问题描述】:
在我有限的知识中,如果不将志愿者作为参数传递给状态方法,我不知道如何编写此代码。这通常不会成为问题,除非我需要所有不规则的志愿者。然后我必须遍历 Volunteers.all 并根据 status 方法评估每个。我觉得这里的开销太大了。
Class Volunteer < ActiveRecord::Base
has_many :reports
def status(volunteer)
case
when volunteer.reports.nought.where(:report_month => Report_range.months(6)).count > 5
return 'inactive'
when volunteer.reports.nought.where(:report_month => Report_range.months(6)).count >= 3
return 'irregular'
else
return 'active'
end
end
end
class Report < ActiveRecord::Base
belongs_to :volunteer
scope :nought, -> {where("hours IS NULL") || where("hours: 0")}
end
module Report_range
def self.months(range)
Volunteer.first.reports.order("report_month desc").limit(range).pluck(:report_month)
end
end
提前感谢您帮助菜鸟!
【问题讨论】:
标签: ruby-on-rails ruby