Ruby on Rails在课程的开始日和中间之间进行选择

时间:2016-09-03 21:23:44

标签: ruby ruby-on-rails-4

你好我正在为健身房开发一个应用程序,在统计数据部分我被困在这个查询中。我需要从数据库中选择从一天开始到13:00之间开始的所有课程,然后计算它们,但我还没弄明白如何制作句子。我最接近的就是这个,我怎样才能选择在整个月的13:00之前开始的所有课程?感谢

@lessons_morning    =   Lesson.where(start_date: (beginning_of_day)..Time.beginning_of_day + 13.hours,updated_at: (Time.now.beginning_of_month)..Time.now.end_of_day)

1 个答案:

答案 0 :(得分:0)

由于您将日期和时间都存储在同一列中,我会这样做:

首先,让我们查询当月的所有课程:

# use Time.zone.now instead of Time.now to use the timezone specified in your application config
@lessons = Lesson.where(
  start_date: Time.zone.now.beginning_of_month..Time.zone.now.end_of_month
)

然后,我们只找到0:00到13:00之间的课程:

@lessons.select do |lesson| 
  start_hour = lesson.start_date.strftime('%H%M').to_i
  start_hour <= 1300
}
相关问题