【发布时间】:2015-01-12 21:41:16
【问题描述】:
坎宁安定律:这必须是完成这项任务的最佳方式。最好的。
# rubies on railz
# Assume we pass an options hash with, possibly, two dates.
# If both dates: do an inclusive between the two
# If start date: do open-ended search
# Else do close-ended search
def my_date_helper(options)
start_date = options[:start_date].try(:beginning_of_day)
end_date = (options[:end_date] || 6.months.from_now).end_of_day
if start_date && end_date
query.where(created_at: start_date..end_date)
elsif start_date
query.where("created_at >= ?", start_date)
else
query.where("created_at <= ?", end_date)
end
end
【问题讨论】:
-
这有问题吗?在事物的宏伟计划中,它似乎是可读的并完成了任务......
-
Nick Veys:感谢您的验证。我们无法想出更优雅的解决方案。有几种写法,但我们发现没有令人信服的理由来改变。我希望我们缺少一些新的 rails helper 或 stdlib 方法。干杯。
-
不知道
query是从哪里来的。
标签: ruby-on-rails ruby date where