Search code examples
ruby-on-railsrubysidekiq

Sidekiq::Rails:Class overrides default Rails class


Sidekiq::Rails:Class override global Rails::Class

I'm trying to call the Rails.cache method but i get an error "undefined method `cache' for Sidekiq::Rails:Class". I check in the debugger and i found that 'Rails'class type in this method is the Sidekiq::Rails:Class and not the default Rails:Class. I think that in runtime sidekiq overrided the Rails main class.

  def index
  ##
     ## >>>>>> Rails here is identified as Sidekiq::Rails:Class
     @cities = Rails.cache.fetch("cities", :expires_in => 10.days) do
       City.all
     end
     render :json => {:list => @cities}
  end


Solution

  • You have to refer to 'root' Rails class, which is possible with :::

    ::Rails.cache.fetch('cities', express_in: 10.days) do
      # ...
    

    To extend my answer, if you have something like:

    module Sidekiq
      # body
    end
    

    it sets the default namespace for the whole body to Sidekiq, so if you want to refer to some constant inside of it, like you did with Rails, Ruby searches for Sidekiq::Rails first and then goes up the namespace hierarchy, searching for ::Rails.