Uninitialized constant when using a module in a user initializer

I am working on a Rails application (3.2) and I need to complete some tasks when loading the application.

Since I want to save the logic in a separate file, I also create lib / helinium.rb that looks (using the false start method)

class Helinium def self.run puts "running ...." end end 

And I created a simple initializer file config / initializers / perform_checks.rb

 Helinium.run 

And everything seems beautiful. Now I want to put the Helinium class in the module so that both files look accordingly as

 module Elemens class Helinium def self.run puts "running ...." end end end 

and

 Elemens::Helinium.run 

but when I try to download the application, I get

uninitialized constant Elemens (NameError)

Am I missing something? Why is the module not found?

Thanks and have a good day.

+4
source share
1 answer

Explanation

This is something to do, how autoload works in Rails.

Rails does not automatically require everything under / lib. It only loads cars when you try to use a new class name that matches the file name in lib.

You can check this post for more information:
fooobar.com/questions/26140 / ...

fixes

To fix the problem, you could

 require 'lib/helinium' 

OR

put the class in a folder named lib\elemens

+5
source

Source: https://habr.com/ru/post/1469426/


All Articles