I’ve got a rails app with models which occur on a weekly basis. Put another way, every user has an instance of the model (let’s say it’s GroceryList) generated once weekly on Monday morning. I’d like to integrate some syntactic sugar into my GroceryList model so that I can easily reference past lists using ruby-esque syntax, perhaps as below:
current_user.grocery_list.1.week.ago
As it stands, each GroceryList item has a created_on date, of course, as well as a start_date which indicates the beginning of the week (Monday).
Is there an established way of tying this type of functionality into a ruby class, and if so how does one achieve it?
Thanks in advance.
This sort of thing is pretty easy to add with named scopes:
Then you can fetch through any relationship using something simple like: