How to model and store recurring tasks in rails?
Asked Answered
U

4

7

Cron solutions in rails are numerous and pretty good. That's not what I'm struggling with here.

Instead, what I'm having trouble with is letting users create their own recurring tasks (like reminders) - specifically how to model and store these in the DB (a good UI for it is non-trivial too - would be awesome if there was code out there for that). Google calendar is a great example here (the UI to add an event, not the entire calendar)...they should be able to do daily at 1pm CST, or mon/wed/fri, or weekly, etc. Whatever cron solution is being used would then need to poll the database to see which reminders needed to be sent at that hour, etc.

Anyone seen a good plugin/gem for this in rails? Seems like there would be something out there for it, but I haven't found it yet.

Urion answered 21/7, 2009 at 4:5 Comment(2)
I'm looking for the exact same thing! Did you find a solution to your problem?Reathareave
see my accepted answer below - rolled my own simple solutionUrion
U
7

I ended up rolling my own solution since I didn't need anything super fancy.

Basically I added a next_run datetime and interval string column to the db which was one of (day, week, etc). Then setup a cron job to run that looks for any next_run dates that have passed. It runs them and then sets next_run to some point in the future based on the interval column.

Simple but worked for my needs.

Urion answered 19/10, 2010 at 18:37 Comment(1)
but how are you calling the next_run ?William
U
1

Currently thinking about using a plugin like this to store recurrences in a table http://github.com/fnando/recurrence/tree/master

each reminder would have one recurrence object, and the reminder would also keep a datetime field when it's supposed to send it's next one. Then the cron could...

get all reminder's whose "next_send" date has passed
for each reminder
  send it
  update the "next_send" field using the recurrence object
end

If there are better solutions or I'm going down the wrong path, input always appreciated.

Urion answered 21/7, 2009 at 5:6 Comment(0)
B
1

I have always found ical (the RFC, not the program) solutions to be the best approach for working with recurring events. There are a few good Ruby libraries for dealing with ical, and the newest kid on the block is ri_cal.

Blackfoot answered 21/7, 2009 at 13:24 Comment(2)
rical looks great and full features, I'm sort of confused on how it might integrate with rails. Does it store events or calendar objects to the database? How would you looking past due events or do queries? Thanks!Urion
unfortunately looks like rical doesn't let you save anything to the database....it's just for reading/generating ical filesUrion
G
0

I'm currently having this problem and the solution that I'm considering is as follows:

class AllowReoccuringTasks < ActiveRecord::Migration  
    def self.up  
        add_column :tasks, :reoccuring, :boolean  
        add_column :tasks, :period, :integer  
    end
end

where period can be either 1 (every day), 7 (every week), or 14 (every other week).

If you wanted to support other types of schedules like every month, weekdays, weekends, etc., you could instead add a column called "schedule" and use constants to represent different schedule types. You could also use the enum plugin.

Granada answered 3/11, 2010 at 3:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.