Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails lib load order

I have the following classes in my applications lib directory:

proxy.rb

class Proxy
end

ga_proxy.rb

class GaProxy < Proxy
  include GaProxy::Metrics
end

metrics.rb

class GaProxy
  module Metrics
  end
end

Load order clearly matters here:

metrics.rb need to be loaded before ga_proxy.rb

proxy.rb needs to be loaded before ga_proxy.rb

But if metrics.rb is loaded before ga_proxy, then I get 'superclass mismatch for class GaProxy' because GaProxy has already been defined without a parent class.

How can I get around this issue?

Thanks

like image 409
99miles Avatar asked Jun 17 '26 07:06

99miles


1 Answers

In your application.rb file, specify each file you want to load in order:

config.autoload_paths += %W( #{config.root}/lib/proxy.rb, #{config.root}/lib/metrics.rb, #{config.root}/lib/ga_proxy.rb )
like image 75
Artem Kalinchuk Avatar answered Jun 19 '26 23:06

Artem Kalinchuk