How nesting and qualified constants work in Rails 6?

In Autoloading and Reloading Constants (Classic Mode) — Ruby on Rails Guides there is a statement to use Option 2

# Option 1
class Admin::UsersController < ApplicationController
  def index
    @users = User.all
  end
end
# Option2
module Admin
  class UsersController < ApplicationController
    def index
      @users = User.all
    end
  end
end

I wonder if the same rule applies to Rails 6 since it uses Zeitwerk. Documentation (Autoloading and Reloading Constants (Zeitwerk Mode) — Ruby on Rails Guides) doesn’t mention anything so I suppose it’s safe to use Option 1.

Note: I like Option 1 since code inside index action eats 4 spaces whilst Option 2 eats 6 spaces in this case (for Api::V1::UsersController it’s 8 spaces).

1 Like

I think this is one of the gotchas that Zeitwerk implementation fixed, so this is why it is not in the guide anymore, but @fxn can confirm better than me.

1 Like

I think this is one of the gotchas that Zeitwerk implementation fixed, so this is why it is not in the guide anymore

Exactly!

I think a lot of people were confused with the difference in the past, but new documentation doesn’t mention it. Does it make sense to note that in the guides?

@ivan.bisevac good idea.

These were documented in the upgrading guide, but your question made me think that people using zeitwerk mode today without upgrading an application may as well wonder about the differences wrt what they knew from the past.

I’ve written a new section in the (modern) autoloading guide covering the most important points, delegating to the upgrading guide for additional edge differences. This section is in master now and has been cherry-picked to 6-0-stable, so it will be out with 6.1 and with the next 6.0.x.

Thanks!

2 Likes

@fxn thanks for the documentation. That solves the confusion perfectly.