Skip to content

Instantly share code, notes, and snippets.

@edwinlab
Last active September 15, 2017 03:00
Show Gist options
  • Select an option

  • Save edwinlab/b237da9bde639c3c53a925b4941b1380 to your computer and use it in GitHub Desktop.

Select an option

Save edwinlab/b237da9bde639c3c53a925b4941b1380 to your computer and use it in GitHub Desktop.
Custom find with date range to handle table partition
module ActiveRecord
class Base
class << self
#
# SELECT "payments".*
# FROM "payments"
# WHERE "payments"."id" = <id>
# AND ("payments"."created_at" BETWEEN '<start month>' AND '<end month>')
# ORDER BY "payments"."id" ASC LIMIT 1
#
def find_between id, date
date = date.to_datetime || Time.zone.now
beginning_month = date.beginning_of_month
end_month = date.end_of_month
where(id: id)
.where(created_at: beginning_month..end_month)
.first
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment