All threads / Recurring events with the ice_cube gem Discussion

Ask A Question


You’re not receiving notifications from this thread.

Recurring events with the ice_cube gem Discussion

On my app I use a Event and a EventTrans model so for each time a event occurs we can have a record with info and other stuff. So for the Calendar we display the First event with the recurrent on the fly and each time the event pass we save it as a record or if we need to move only on recurrent we create it as a event transaction.

My only problem is the time it takes to check if a recurrent is create as event_trans or not for calendar view.


For some reason the simple_calendar gem messed with a bunch of the CSS on my site (e.g. all my links now have a black background on hover, etc.). Any hints on how to avoid this?

simple_calendar doesn't include any CSS for the links, so it must come from somewhere else. It only has some optional CSS for styling the calendar, but not links.

Sounds like you may have created a scaffold and the scaffold.css file is styling your links.

Gaaahhh! Thank you! That was driving me crazy.

Alex Musayev ·

Thanks for a good demo for IceCube! I've been looking for this functionality to use it in one of my pet projects, but ended up with implementing something like this using cron format to represent recurrent event rules, and parse-cron gem to parse it. But now I'll try to switch to IceCube. I like the hash format it uses. It look less cryptic than crontab. Thanks for a good tip :)

Jorg Dominguez ·

Waiting for the next episode!!!
You are great!


Really enjoying this series. Curious to when the next episode will be for exceptions etc?

Hey Josh, looks like I forgot to put the video in the series. You can find the episode here:

Fantastic! Thanks @excid3:disqus much appreciated. Are you looking to extend on this gem any further or do any further Vlog's?
Keen to have another view for possible listing and also if we needed to associate another model to a event recurrence?


Um possibly. I just made a tiny tweak today that improved performance by like 4x or something. Thought that might be useful to talk about, but I think I've covered memoization before.

What do you mean by "view for possible listing"? And if you want to do two models, you could combine the query results into what you pass in for the events array and as long as they have the "start_time" interface, it doesn't care what type of objects they are.

Hi @excid3:disqus looking to try and implement views like this
That's what I mean by "view for possible listing?"

Also when you click on Week, Month etc it provide Event summary like number of events in that month/week.

Would be a good for extending on this series also :)

Ah yeah, that's what I figured. I'll consider it, one of the reason I haven't is that I didn't want any JS to be required. You could easily just make a route for each of those and them add your own buttons to each of the views and turbolinks would make navigating between those pretty snappy.

It does probably make sense to add day and year views to the calendar and leave it up to you to link them up on the page.


Hi, thanks for the tutorial I found it very helpful. I wondered if you had any thoughts on the best way to query recurring events that have been serialised using the recurring gem? The issue I'm running into is if an individual who has not created the original calendar events wanted to search several calendars (created by other users) for free slots at given times the serialiser method then seems to become more complex than if we'd created individual discrete events. For example if you wanted to share calendars between different users. Would be interested to know your thoughts on this. Thanks

Depends on what you're trying to accomplish (like everything right? lol). What kind of search are you trying to pull off?

Thanks for your reply. Lol a little bit. I'm trying to make a basic scheduling app so that different users can search each others calendar schedules for available time slots and arrange meetings. From the user's standpoint; each user adds events to their calendar which take up a time slot. Their availability is then determined to be those time slots with no events (in practice slightly more complex than this but for now this is what I'm looking at). Then the user's search (of another user's calendar) would be something like:

"All time slots (for the another user) from datetime x to datetime y across one calendar with no events present".

The purpose would be to schedule a meeting for instance. The way I thought of the problem was to put all events matching given search date range criteria into an array that would appear as "search results". This would avoid the necessity of going into every user's calendar manually to check availability that way. It's quite simple to do that with discrete non-recurring events as you are querying discrete objects out of a database. But because the serialise methodology is not generating all the events in a series at once unless you load up the calendar for each user it seems a little more complex to do that.

Ah yeah, that's a pretty complex one. I think what I would probably do is this:

1. Query for all the individual events
2. Query for all the recurring events within that range. This would match recurring events where the start date is before the end of the query and end dates of null (infinitely recurring) or end date after the beginning of the query.

Then I'd load those recurring events into ice_cube and then run the generator to create all the events for the recurring ones within your query range.

3. Then you can combine those two arrays of events into one and your view will have an accurate set of event objects to use for that query range.

Obviously, it's not as simple as the alternative of making recurring events just insert regular event records into the database. That may be good enough for your solution, or you may want truly infinite recurring events which would require you to build a bit more of a complex query system like above.

Thanks, much appreciated that makes sense I'll give it a shot. I'm realising I have to weigh up the advantage of saving space by accessing serialised events versus the convenience of querying actual event records in DB. I really liked the way you used recurrence select with ice cube and wanted to keep that approach.

Yeah, I can see going both ones. On one hand, the developer effort is easier to just generate and save the recurring events but it makes the user experience a lot tougher (how do remove them all? what if you want to edit all of them? what about just one?). And then on the other hand, it's easier for the user, but tougher on you as a developer to build the ice_cube style recurring events and query for those.

You'll probably be fine either approach you take, just remember it can always be changed and improved later on so it all depends on what business needs are most important. :D

Sorry to bother you again but would really appreciate any tips of adapting this. I was trying to adapt your calendar_events method for use in saving all the instances of a recurring event from the events controller create method. Any idea how I could adapt this to generate and save all recurring events for a new instance of an event? My create method in event controller code is below. I think I just need to tweak it a little as it works for all the existing saved events but i specifically just want it to pass the new event through to the method.

My aim is this: user creates new event with recurrence rule, then on save it loops through all occurrences of this instance up until some predefined end time creating an array of the associated events which are subsequently saved after being generated. The difference here is that rather than fetching all the already saved events and looping through the occurrences, I want to do that only for the new instance that the user is creating when they open up the form.
/////events controller code

def create
## first testing for whether recurring is null or not
#room have one to many relationship to events

if params[:event][:recurring] == "null"

room = Room.find(params[:room_id])
@event =

room = Room.find(params[:room_id])
#this loops through all already saved events and #then creates an array of occurances for each saved #event whereas I am trying to do this for only the #instance of the event being created not every event #already saved

@calendar_events ={ e|e.calendar_events(params.fetch(:start,}



///event modelcode

#I made a modification to the calendar_events method to account for one to many relationshi with rooms to events, and I am using start instead of start_date

def calendar_events(startit)
if recurring.empty?

end_date = startit.end_of_month.end_of_week

schedule(start).occurrences(end_date).map do |date|

@event = room_id, title: title, start: date, end: date, color: color, recurring: recurring)


This isn't too bad. You would probably be well served to move your code for creating the individual events to the model itself so that this logic doesn't clutter the controller.

It looks like you're on the right track. I think you can clean a lot of this up like so:

This is untested, but I think the only issue with this would be that you would need to probably skip that first event in the recurring loop as it probably would duplicate the initial event.

Thanks again, much appreciated I'll try that. Cheers

Shawn Nigel Rebello ·

Thanks a lot for this !

Has anyone gotten recurring_select to work on rails 5.2?? I keep getting a wrong number of attributes error no matter what I try to pass it.
Seems like it only occurs on Rails 5.2.rc2.. I'm not sure why though.
Looks like there was a change in the value method of the FormBuilder Base class. I changed it to not pass de object around anymore on a fork, but this would make the gem not work with previous versions... anyways, I'll leave the link here in case anyone is in need of a quick fix.
Good work! Looks like this probably changed somewhat recently then. The gem sure hasn't been updated for quite a while so it's surprising other things haven't broken!

Hello Chris, thank you so much for putting this leson together.
I followed it step by step and everything worked fine, that is until something got mistrerously broken and now I cannot seem to know what it is or how to put it back together.

Coould you please take a look to the issue I posted on github and let me know your thoughts?




Thanks Chris, very helpful as always :-)

As RecurringSelect.is_valid_rule?('null') is true, I had to change the recurring=(value) method.

We did you change? I am getting the error "undefined method `to_hash' for nil:NilClass" And I was wondering if that could be the issue.


Make sure you don't have old event data stored in the event table as you move through the excercise. Also, as Chris Seelus pointed out, latest recurring select does parse string null and makes it a valid rule.

Maybe put a guard to call super(nil) like so?

def recurring=(value)
    if value != "null" && RecurringSelect.is_valid_rule?(value)

Hi Chris!

Thank you for the episode. It was very educational. 8-) Ross Kaffenberger (@rossta) has a neat gem called Montrose ( similar to/inspired by ice_cube which models recurrence quite nicely as well.

I am stuck in the ice_cube world though trying to solve the following problem: My event model needs to be searchable/order-able. Since I did not want to reinvent the wheel, I ended up with datatables implementation. I created a nice JSON endpoint which produces events. I was cruising along with implementation, until I got to the actual search part. The JSON endpoint returns data which is ordered/searched via chained AR calls, something like:

def fetch_events
  search_string = []
  %w[name description city country state zipcode].each do |term|
    search_string << "#{ term } ILIKE :search"

  events ="#{sort_column} #{sort_direction}")
  events =
  events = events.where(search_string.join(' OR '), search: "%#{params[:search][:value]}%")

Soooo, it seems that if I want to include recurring events in the JSON endpoint, I should inject Event#calendar_events in the code above. One could, do that at the end, however, that wouldn't jive with sorting part. Not sure if one can try to do it early in the chain, becase, well, AR.

Would you, or anybody elase, have a clever idea how to handle that?

Thank you,



I am using ice-cube gem for recurring.
I have a form which allows user to select datetime for recurring.
Now I want to schedule and run a background job based on user frequncy.
Like if the frequncy is daily hten that job should run daily. How can I achieve this using ice-cube gem.


Any suggestions on how to handle searching the database for overlapping events when creating a new event?

With all the recurring info in the serialized column it seems there is no way to leave the heavy lifting to the database, and instead we need to iterate all events, find their recurring time slots, and compare. Is there any other solution?

Yeah, there's not a real easy way when the schedules are dynamically generated.


I've installed the gem exactly as the video says, however <%= form.select_recurring :recurring, nil, allow_blank: true %> throws an error "wrong number of arguments (given 1, expected 0)"

It's as if the gem hasn't installed and can't find the form helper.
Does anyone know where to go from here?

I'm using:
rails 6.0.1

And installing the gem like so:
gem 'recurring_select', github: "RundownApp/recurring_select"

I've also run bundle with no issues.

Out of curiosity, I created a new rails app exactly as Chris has here and I am getting the same error :(

Any help would be greatly appreciated :) Cheers guys

As was mentioned in the video, github: "RundownApp/recurring_select" was a fork for rails5. If you're having issues, try which has been updated for rails 6


Hi Chris. This episode was exactly what I was looking for, thank you.
I've followed a few of your tutorials now and find them great.

I have been trying to figure out a way of marking individual instances of a recurring task as complete.
Has anyone tried/succeeded with this?


Thank you for sharing this video! I am having a really tough time with my application. When I go to create a recurring event, the javascript is not coming up to allow me to select from the dropdown menu...I have been searching online for hours trying to figure this out and nothing has worked :( any ideas what the problem could be?

Hi, Emma you need to first check that the script is running and that turbolinks is not blocking the recurring event. I use google chrome and the dev terminal to see if its running, there's different ways to check.

Thank you for your response Francisco! I disabled Turbolinks to see if it was blocking the recurring event, but still no luck. I'm wondering if the problem has something to do with the fact that I'm using rails 6 and this video was posted using rails 5 I believe...I am still fairly new to rails, so this is the most complex item I've implemented in my application thus far.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 52,887+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2021 GoRails, LLC. All rights reserved.