假设我有一个名为 Absence 的模型,看起来像这样
:id => :integer,
:employee_id => :integer,
:start_date => :date,
:end_date => :date
我需要检查员工今天是否离开,如果离开则返回 true。如果某人有缺勤记录,则他不在
- 开始日期是今天或今天之前,
- 结束日期为 null、今天或早于今天。
所以我需要一个类似于 Employee 的方法
def is_away
?????
end
请帮忙!
最佳答案
我会做这样的事情:
# add this to absence.rb
def covers_today?
(start_date..end_date).cover?(Date.today)
end
# add this to employee.rb
def away?
absences.any?(&:covers_today?)
end
完成此操作后,只需调用 employee
的 away?
即可:
@employee.away?
# => true, if employee has an absense that covers the current date
关于ruby-on-rails - ruby 检查当前日期是否在日期记录内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25549547/