找到你要的答案

Q:Rails ActiveRecord time between 10:00 - 00:00

Q:Rails的ActiveRecord之间时间10:00 - 00:00

rails 4.2.3 with PostgreSQL.

DB table company has fields:

start_day_time: string end_day_time: string

that store hours:minutes like:

start_day_time: 10:00, end_day_time: 23:00

ActiveRecord where expressions with comparison by this fields works well. I can find currently open company easily

today_date = Time.zone.now
today_date_time = today_date.strftime('%H:%M')
Company.where(
'start_day_time < ? AND end_day_time > ?', today_date_time, today_date_time
)

The problem

But comparison fail when end_day_time is 00:00. It works only if i use 23:59.

Is there are way to fix this? Can i use additional rule in my sql expression to turn 00:00 to 23:59 temporary only for this query?

The simplest way – do not store 00:00 in this fields. But they used in user interface to present company work hours.

Rails 4.2.3 PostgreSQL。

DB表公司有字段:

start_day_time: string end_day_time: string

那个商店小时:分钟一样:

start_day_time:10:00,end_day_time:23:00

他在这个领域的作品表达的比较好。我很容易找到目前开的公司

today_date = Time.zone.now
today_date_time = today_date.strftime('%H:%M')
Company.where(
'start_day_time < ? AND end_day_time > ?', today_date_time, today_date_time
)

问题

但比较失败的是当end_day_time 00:00。我只有用23:59作品。

Is there are way to fix this? Can i use additional rule in my sql expression to turn 00:00 to 23:59 temporary only for this query?

最简单的方法–不存储在这个领域00:00。但他们使用的用户界面,目前公司的工作时间。

answer1: 回答1:

I just recently did something like this.
In my model I created a method called happening_now?

This is what it looks like

  def happening_now?
    Time.now.strftime('2000-01-01 %H:%m:%S').between?(self.starts_st, self.ends_at)
  end

I hope that this helps.

I just recently did something like this.
In my model I created a method called happening_now?

这就是它看起来像

  def happening_now?
    Time.now.strftime('2000-01-01 %H:%m:%S').between?(self.starts_st, self.ends_at)
  end

我希望这有助于。

mysql  ruby-on-rails  ruby  postgresql  activerecord