简体   繁体   中英

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM