Change where module file goes for namespaced AR models

Hello all!

Because Rails apps get cluttered up rather quickly, I am proposing a change to this Rails generator method:

https://github.com/rails/rails/blob/master/activerecord/lib/rails/generators/active_record/model/model_generator.rb (starting at LOC 29)

def create_module_file return if regular_class_path.empty? template “module.rb”, File.join(“app/models”, “#{class_path.join(‘/’)}.rb”) if behavior == :invoke end

``

TO

Enter co

` de here…

def create_module_file return if regular_class_path.empty? template “module.rb”, File.join(“app/models”, class_path, “#{class_path.join(‘/’)}.rb”) if behavior == :invoke end`

Some more background and argument:

when you do something like: rails g model admin/dashboard rails g model user/dashboard user/profile

``

You’ll get nicely namespaced AR models and tables. The AR models are placed into a sub-directory within the models directory and a module is placed into the root model directory. The module only contains the table_name_prefix. So why not put that module into the sub-directory with the model(s)?

Thanks for your time!

For concreteness, are you suggesting that the first command there generate the file at app/models/admin/admin.rb? AFAIK that’s not going to work with the existing autoloader - it won’t look there by default, and it would expect a file located there to define a class or module named Admin::Admin if it did.

—Matt Jones

You are right about the autoloader. I have been putting those files in `app/modules` and wasn't thinking about the autoloader when I changed my suggestion to suggest using the subdirectory inside `app/models`. I made that change before posting this new topic because I felt that suggesting a whole new dir wouldn't be well received.

This is only a problem when you're dealing with large monoliths, and because that is a result of poor app design, this suggestion is probably not warranted after all.