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 convention the model class is namespaced into an according module.
E.g.
app/models/blog/post.rb
app/models/blog/comment.rb
app/models/user.rb
so that:
app/models/blog/post.rb
class Post < ActiveRecord end
and not
class Blog::Post < ActiveRecord end
Here is what I used for Rails 3:
config.autoload_paths += Dir[Rails.root.join('app', 'models', '{**}')]
This configuration tells Rails to scan all the app/models subfolders recursively and load all found models. No namespacing required.
We needed to do this, and there is a very simple way.
move your models into the sub-folders, and then tell rails to load files from all subfolders in your environment.rb file:
config.load_paths += Dir["#{RAILS_ROOT}/app/models/*"].find_all { |f| File.stat(f).directory? }
No namespacing required, and the models can be referred to as normal in your app
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With