少於 1 分鐘閱讀

MODEL

scope :with_year_and_month, ->(year, month) {
  date = DateTime.new(year,month)
  where(created_at: date...date.next_month)
}

# => created_at >= '2015-02-01 00:00:00' AND created_at < '2015-03-01 00:00:00'

參考

  1. http://stackoverflow.com/questions/28642796/selecting-entries-with-a-specific-year-and-month-from-a-rails-database

更新時間:

留言