Elegant way to structure models into subfolders without creating submodules

前端 未结 10 544
一整个雨季
一整个雨季 2020-12-04 08:24

I have numerous models in my app/models folder. I\'d like to clean this folder up a little bit. Move models that belong to each other in subfolders. The problem is that by c

10条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-04 09:17

    I also created subfolders, and then added the following to the application.rb file:

    config.autoload_paths += Dir["#{config.root}/app/models/**/"]
    

    But doing this alone isn't enough when subfolders are named using the same name as a model (e.g., a folder 'user' containing several files, one of which is 'user'). This was causing all kinds of errors in my code until I found that it could be solved by simply giving the folders names that are different from the models (e.g., 'user models') they contain. I found the suggestion at http://www.williambharding.com/blog/technology/rails-3-autoload-modules-and-classes-in-production/, which actually points to this question.

提交回复
热议问题