Ruby on rails 如何将日期与多种条件进行比较?
我试图将一个日期与多个条件进行比较,并根据它满足的条件返回一个特定的输出。我不喜欢这些Ruby on rails 如何将日期与多种条件进行比较?,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我试图将一个日期与多个条件进行比较,并根据它满足的条件返回一个特定的输出。我不喜欢这些if和elsif语句: def ugly_method if first_active_entitlement.try(:created_at) == nil "No timestamp on first active entitlement" elsif first_active_entitlement == nil "No Active Entitlement"
if
和elsif
语句:
def ugly_method
if first_active_entitlement.try(:created_at) == nil
"No timestamp on first active entitlement"
elsif first_active_entitlement == nil
"No Active Entitlement"
elsif last_suspended_entitlement == nil
first_time_or_renewal(first_active_entitlement, start_date, end_date)
elsif first_active_entitlement.try(:created_at) > last_suspended_entitlement.try(:suspended_at) && first_active_entitlement.try(:created_at) > start_date && first_active_entitlement.created_at < end_date
"Restart"
else
first_time_or_renewal(first_active_entitlement, start_date, end_date)
end
def_方法
如果第一个\u活动\u权限。try(:created\u at)=nil
“第一个活动授权没有时间戳”
elsif第一个有效权利==零
“无有效授权”
elsif上次暂停的权利==零
首次或续签(首次有效权利、开始日期、结束日期)
elsif first\u active\u authority.try(:created\u at)>last\u suspended\u authority.try(:suspended\u at)&&first\u active\u authority.try(:created\u at)>start\u date&&first\u active\u authority.在
有没有人必须将一个日期与许多不同的条件进行比较?我错过的任何明显的东西会更容易吗 我会将其分解为多种方法:
- 验证
first\u active\u权限
- 验证
上次\u暂停\u权利
然后,在您的方法中,您可以显示来自这些方法的消息,以防该方法的标志设置为false(如果不只是执行续订)。无需感谢我们的澄清。这是为了帮助社区。