我有选择班次的余地:
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
}
更清洁的方法吗?
答案 0 :(得分:2)
scope :by_date, lambda { |date|
where("DATE(start_date) = :date OR DATE(end_date) = :date", date: date.to_date)
}
答案 1 :(得分: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)
}