title: Rails url_for and namespaced models
body: <p>In Rails, is it possible to namespace models in modules and still get correct behavior from <code>url_for</code>?</p>
  
  <p>For instance, here, <code>url_for</code> works as expected:</p>
  
  <pre><code># app/models/user.rb
  class User &lt; ActiveRecord::Base
  end
  
  # config/routes.rb
  resources :users
  
  # app/views/users/index.html.haml
  = url_for(@user)    # /users/1
  </code></pre>
  
  <p>Whereas after putting the <code>User</code> model into a module, <code>url_for</code> complains about an undefined method <code>m_user_path</code>:</p>
  
  <pre><code># app/models/m/user.rb
  module M
    class User &lt; ActiveRecord::Base
    end
  end
  
  # config/routes.rb
  resources :users
  
  # app/views/users/index.html.haml
  = url_for(@user)    # undefined method 'm_users_path'
  </code></pre>
  
  <p>Is it possible to have <code>url_for</code> ignore the module in <code>M::User</code> and return <code>user_path</code> for <code>url_for(@user)</code> instead of <code>m_user_path</code>?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>So, after almost 5 years, here's the solution, thanks to esad. This has been tested in Rails 4.2.</p>
  
  <pre><code># app/models/m/user.rb
  module M
    class User &lt; ActiveRecord::Base
    end
  end
  
  # app/models/m.rb
  module M
    def self.use_relative_model_naming?
      true
    end
    def self.table_name_prefix
      'm_'
    end
  end
  
  # config/routes.rb
  resources :users
  
  # app/views/users/index.html.haml
  = url_for(@user)    # /users/1
  </code></pre>
  
  <p><em>Note: when generating model, view and controller with <code>bin/rails g scaffold m/user</code>, the views and the controller will be namespaced, too. You need to move <code>app/views/m/users</code> to <code>app/views/users</code> and <code>app/controllers/m/users_controller.rb</code> to <code>app/controllers/users_controller.rb</code>; you also need to remove references to the module <code>M</code> everywhere except in the model <code>M::User</code>.</em></p>
  
  <p>Finally, the goal here was to namespace models but not views and controllers. With esads solution, the module <code>M</code> (containing <code>User</code>) is explicitly told to not appear in routes. Thus, effectifely, the <code>M</code> is stripped of and only <code>User</code> remains.</p>
  
  <p>The user model can now reside in <code>app/views/models/m/user.rb</code>, the users controller lives in <code>app/views/controllers/users_controller.rb</code> and the views can be found in <code>app/views/users</code>.</p>