0

我有选择班次的余地:

scope :by_date, lambda { |date|
  where "end_date BETWEEN ? AND ? OR start_date BETWEEN ? AND ?", date.beginning_of_day, date.end_of_day, date.beginning_of_day, date.end_of_day
}

是更清洁的方法吗?

4

2 回答 2

2
scope :by_date, lambda { |date| 
  where("DATE(start_date) = :date OR DATE(end_date) = :date", date: date.to_date) 
}
于 2013-09-13T20:00:53.087 回答
1

这个怎么样:

scope :by_date, lambda { |date|
  where("end_date BETWEEN :beginning AND :end OR start_date BETWEEN :beginning AND :end",
    beginning: date.beginning_of_day, end: date.end_of_day)
}
于 2013-09-13T10:43:02.080 回答