A namespace is a way to group related functionality together in a modular way. Rails 7 has improved the support for the custom namespace, now there is a better way of adding the custom namespace to application components while autoloading.
Rails by default autoloads all the subdirectories in the
app folder except
Even if there is a custom directory inside the app, for example,
don’t need to configure anything to autoload it, Rails does it automatically.
In a Rails application, file names have to match the constants they define,
with directories acting as namespaces.
For example, the file
app/services/users/signup.rb has to define the constant
But what if we would like all our services in
namespace, it means that we would want to
define a constant like
One way to accomplish this is to create a subdirectory
then we can put all our services under the services namespace.
But this doesn’t feel right, there should be a way to define file
app/services/users/signup.rb constant like
Now there are two modes of autoloading files in Rails, one is the
and the other is
classic mode we can define constant in both ways for a
app/services/users/signup.rb. It means that
Users::Signup are valid constant names.
But it creates ambiguity.
zeitwerk mode we can only define
there is a workaround.
Zeitwerk supports custom root namespaces
and we can customize main autoloader.
In the above code, we created a
Services namespace, we can create it like this or
we can create it elsewhere and then
require it here.
Next, we used the
main autoloader to do the task.
push_dir method takes a module or class object as the second argument.
Applications running on Rails < 7.1 have to additionally delete the
Now after this patch, we can add the custom namespace
creating a subdirectory like
We just have to customize the main autoloader like below and that is it.
There is no need to delete
For more information about this patch, please go through