Why do I get a NoMethodError when calling the occurring_at? method on a schedule with the ice_cube gem in Rails 7

88 Views Asked by At

In my Rails 7 app, I have installed the ice_cube gem (with the following line in the Gemfile):

gem "ice_cube"

At the top of my batch model, I have added the following lines:

require "ice_cube"
require "active_support/time"
serialize :schedule, Hash

The schedule column is a text column, and I have defined the following create_schedule method in the batch model:

def create_schedule
  schedule = IceCube::Schedule.new(self.start_datetime, duration: duration(start_datetime, end_datetime))
  schedule.add_recurrence_rule(IceCube::Rule.daily)
  self.schedule = schedule.to_hash
end

I call the create_schedule method in the create action of the controller:

def create
  @batch = Batch.new(batch_params)
  @batch.create_schedule
  @batch.user = current_user
  if @batch.save
    redirect_to batch_tasks_path(@batch), notice: "Batch sucessfully created."
  else
    render :new, status: :unprocessable_entity
  end
end

This appears to work, since I can check in Rails console that the schedule attribute contains a value in the expected format, for instance:

"{:start_time=>{:time=>2022-08-05 09:33:00 UTC, :zone=>\"UTC\"}, :end_time=>{:time=>2022-08-05 10:33:00 UTC, :zone=>\"UTC\"}, :rrules=>[{:validations=>{}, :rule_type=>\"IceCube::DailyRule\", :interval=>1}], :rtimes=>[], :extimes=>[]}"

However, whenever I try to call an occurrence method on the schedule, such as batch.schedule.occurring_at?(Time.now), it returns an error like this:

undefined method `occurring_at?' for "{:start_time=>{:time=>2022-08-05 09:33:00 UTC, :zone=>\"UTC\"}, :end_time=>{:time=>2022-08-05 10:33:00 UTC, :zone=>\"UTC\"}, :rrules=>[{:validations=>{}, :rule_type=>\"IceCube::DailyRule\", :interval=>1}], :rtimes=>[], :extimes=>[]}":String (NoMethodError) 
    
b.schedule.occurring_at?(Time.now)                                                     
          ^^^^^^^^^^^    
       

I have tried other ice_cube methods, such as occurrences, occurs_on?, etc. but none seems to be defined: it's as if the ice_cube methods were not available at all in my app.

What I am missing here?

0

There are 0 best solutions below