Rails查询 - 如何比较两个日期的月份?

时间:2017-08-24 08:37:10

标签: ruby-on-rails sqlite

我知道查询之间的rails支持。

[实施例] 性能(有一个start_date,end_date)

我想在几个月之前带来表演。

First performance (2017.07.01~ 2017.08.31)

Second performance (2017.08.08~ 2017.08.20)

Third performace (2017.08.09~ 2018.08.09)

我想获得2017. 08表演..

Rails查询 - 如何比较两个日期的月份?

我认为

@performances = Performance.where("? BETWEEN start_date(* change month?) AND end_date(* change month?)", @filter_date(* "2017-08 DATE").month)

已更新

  beginning_of_month = @filter_date.beginning_of_month
  end_of_month = @filter_date.end_of_month
  @performances = @performances.where("(start_date BETWEEN ? AND ?) OR (end_date BETWEEN ? AND ?) OR (start_date < ? AND end_date > ?)", beginning_of_month, end_of_month, beginning_of_month, end_of_month, beginning_of_month, end_of_month)

1 个答案:

答案 0 :(得分:0)

我不知道Rails查询,但这样怎么样?

(start_date between 2017.08.01 and 2017.08.31) or
(end_date between 2017.08.01 and 2017.08.31) or
(start_date < 2017.08.01 and end_date > 2017.08.31)