0

目前我有以下行:

scope :within_date_range, ->(start_date, end_date) {
  where("(start_date BETWEEN ? AND ?) OR (end_date BETWEEN ? AND ?)", start_date, end_date, start_date, end_date)
}

有没有更优雅的方式使用类似的东西来做到这一点?

scope :within_date_range, ->(start_date, end_date) {
  where((:start_date => start_date..end_date) OR (:end_date => start_date..end_date))
}
4

1 回答 1

0

使用阿雷尔

t = Post.arel_table
Post.where(t[:start_date].eq(start_date..end_date).or(t[:end_date].eq(start_date..end_date)))

或者

Post.where('start_date BETWEEN (:start_date) AND (:end_date) OR end_date BETWEEN (:start_date) AND (:end_date)', :start_date => (start_date..end_date), :end_date => (start_date..end_date))
于 2013-08-09T07:46:44.153 回答