Adding Recurrence Rule to Generated ICS file in Rails

If you need to integrate your Rails app with some calendaring service, such as Google Calendar, sooner or later you will find yourself writing code that outputs your Event object as an ICS file. But if your events have recurrence rules, you are out of luck, the popular Icalendar gem doesn’t support them. I’ll show how to make it work.

ICS is a calendar standard supported by a wide range of Calendar applications. In my case, I needed to attach ICS file to outgoing emails that are sent to users every time a new event is scheduled. The reason? The nice “Add to your Calendar” box that is added automatically in Gmail and other clients, that allows the user to quickly add the event to his calendar.



Icalendar gem is a simple library that allows populating an Icalendar::Event object and outputting it as ICS string.


But it’s missing a crucial component – a support for recurring events. After some searching, I ended up writing a build_recurrence_rule method that builds a recurrence rule from my Event object (referred as self in the code)

Now, all that is left is to somehow add the generated recurrence rule to the Icalendar::Event object we created earlier and output the result as a string.

And now, all you have to do is add the generated ICS as an attachment in your mailer:

And you are good to go.

Leave a Reply

Your email address will not be published. Required fields are marked *