Elegant way to structure models into subfolders without creating submodules

前端 未结 10 549
一整个雨季
一整个雨季 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:15

    All the above answers didn't work for me. Somehow 'models' folder was loaded with subfolders, which resulted in 'Expected to contain ::.

    Most of my subdirs were STI classes so I've moved them to app/models_sti//*. Then all I needed to do was to put in application.rb

    #config.autoload_paths += Dir["#{Rails.root.to_s}/app/models/**/"]
    # Loaded dynamically (cache_classes == false ?)
    config.autoload_paths << Rails.root.join('app', 'models').to_s
    config.autoload_paths += Dir["#{Rails.root.to_s}/app/models_sti/*"].find_all { |f| File.stat(f).directory? }
    
    # Eager_load_paths - used only when cache_classes == true [rails spec says so]
    config.eager_load_paths.delete_if do |path|
      # If I didn't delete it from eager_load_paths I got errors even in develop
      path == Rails.root.join('app', 'models_sti').to_s
    end
    config.eager_load_paths += config.autoload_paths
    

提交回复
热议问题