For a project I'm currently working on the user needs to be able to add a specific date, weekly dates (e.g. every wednesday 12pm), or monthly dates, (e.g. every 1th of the month 8pm). The dates are the primary thing a user would search for. And that the fi开发者_如何转开发rst upcoming date is findable is the most important.
Thought about it for a while, but I'm not sure where to go with it. This is something I came up with:
Add a certain amount of records for the giving dates, and update these every day, or week.
Problem here is, what do I do when a user searches for 2020 and the record isn't added because it is such a long time from now. Or what if the time is changed. How do I easily find the right records back?
I don't think adding multiple entries for each dates is a feasible solution, in part because of the issue you described. It's also potentially going to be a very quick way of blowing out your database size, particularly if there are a lot of users/dates.
My first thought would be to move away from the timestamp columns for this. This is in part so that you don't have to create multiple records, but also to make querying a lot easier.
I would separate logical units into different columns. Something like this, for the dates you described:
id | wday | mday | hour | minute |
----------------------------------
1 | 3 | NULL | 12 | 00 |
2 | NULL | 1 | 20 | 00 |
This way, it would be very easy for you to find dates for Wednesdays (Model.where(:wday => 3)
) or the first of every month (Model.where(:mday => 1)
), and it would also allow you to display results for any given year, month, or week, etc, for the rest of eternity.
精彩评论