Last active
September 15, 2017 03:00
-
-
Save edwinlab/b237da9bde639c3c53a925b4941b1380 to your computer and use it in GitHub Desktop.
Custom find with date range to handle table partition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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