1

In rails 5.2 I have a small lib which is under app/lib/itunes (everything under app/lib should be autoloaded right?). Nevertheless I get a load error when I start sidekiq

    LoadError: Unable to autoload constant Itunes::ItunesClient, 
expected /app/lib/itunes/itunes_client.rb to define it
2018-06-06T19:38:49.560Z 46606 TID-ov5d572iq WARN: 
.rvm/gems/ruby-2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:503:in `load_missing_constant'
.rvm/gems/ruby-2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:193:in `const_missing'
/app/interactors/fetch_itunes_app_service.rb:4:in `call'



class FetchItunesAppService
  include Interactor
  def call
    @client = Itunes::ItunesClient.new
    ...
  end
end


#app/lib/itunes/itunes_client.rb    
class Itunes::ItunesClient < Itunes::ItunesBaseClient

...
end

#app/lib/itunes/itunes_base_client.rb
class Itunes::ItunesBaseClient
...
end
dc10
  • 2,160
  • 6
  • 29
  • 46
  • @jvillian I do need a namespace. Furthermore it shouldnt make any difference to have another subfolder, since the same naming works with models – dc10 Jun 06 '18 at 19:56
  • 1
    Did by `app/lib/itunes` you mean `app/lib/itunes/itunes_client.rb`? – Andrey Deineko Jun 06 '18 at 20:01
  • @AndreyDeineko right. it somehow feels odd to me to put all the libs right under app since i will end up having all the libs mixed with controllers views and models – dc10 Jun 06 '18 at 20:02
  • Did you stop your server, do `spring stop` and restart your server? – jvillian Jun 06 '18 at 20:07
  • I don't use spring in this case. I started the app with rubymine in debug mode – dc10 Jun 06 '18 at 20:10
  • 2
    `config.eager_load = true` from [here](https://stackoverflow.com/a/40019108), maybe? For me it worked. – iGian Jun 06 '18 at 20:10
  • eager_load ist set to false in development but I have the following: %w(lib presenters interactors finders strategies services serializers).each do |dir| config.autoload_paths += %W(#{config.root}/app/#{dir}) end – dc10 Jun 06 '18 at 20:13
  • 1
    eager_load = true fixed it ,so adding the lib folder to autload_paths is apparently not sufficient?! What is the impact of eager_load set to true? – dc10 Jun 06 '18 at 20:18
  • 1
    @dc10 it would like for your class to be nested inside the module e.g. `module Itunes; class ItunesClient < ItunesBaseClient;end;end` it has to do with the way constant look up and module nesting works in ruby. This [Article](https://cirw.in/blog/constant-lookup.html) does a nice job of explaining it. When you eager load the class is loaded into the global namespace as `Itunes::ItunesClient` rather than loading the module `Itunes` into the global name space and loading `ItunesClient` into the `Itunes` namespace. While these seem similar they have a different impact – engineersmnky Jun 06 '18 at 20:28
  • @engineersmnky I tried the module syntax as well and it was not taking effect. nevertheless the eager_load setting fixed it – dc10 Jun 06 '18 at 20:31
  • I'm getting this error: https://stackoverflow.com/questions/44465118/rails-5-1-unknown-firstpos-nilclass-issue-reloading-application maybe due to `config.eager_load = true`. Aren't you? – iGian Jun 07 '18 at 18:58
  • yes, I got this one as well, I had to stop spring – dc10 Jun 07 '18 at 19:30

0 Answers0