title: Nesting class in module vs using module as part of class name
body: <p>Similar to <a href="https://stackoverflow.com/questions/32396212">module and class with the same name in Rails project</a>, but I'm trying to understand why putting it all on one line works but using separate lines does not.</p>
  
  <p>Working with inviting new users to an account. My <code>user</code> model has the minimum validation, and I use subclasses with additional behavior. This is similar to service objects, but subclasses are cleaner than services most of the time because they are all kept in the <code>/app/models</code> folder and is easier to maintain.</p>
  
  <p>I have <code>app/models/user/as_invitation.rb</code>. When it looks like the following, it works fine:</p>
  
  <pre><code>class ::User::AsInvitation &lt; ::User
    # ...
  end
  </code></pre>
  
  <p>But when I have this, it does not work: "User is not a module".</p>
  
  <pre><code>module User
    class AsInvitation &lt; ::User
      # ...
    end
  end
  </code></pre>
  
  <p>My suspicion is that Ruby uses the same namespace for modules/classes, and since I already have a <code>User</code> class (model), it collides with the <code>User</code> module (module vs model).</p>
  
  <p>I can use the <code>::User::AsInvitation</code> format and it works, but it bugs me when magic happens. As another solution, I can use the module name <code>Users</code> plural, but it makes more sense to me to use singular <code>User</code> as the namespace.</p>
  
  <p>I'm trying to understand a bit under-the-hood of how Ruby/Rails implement this as well as determine if I go ahead and use <code>::User::AsInvitation</code> on one line, whether I am going to find myself down a river without a paddle.</p>