Rails: Elegant way to structure models into subfolders without creating submodules?

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.

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.

This doesn't work in Rails 3. Any ideas? – Vincent Sep 17 '10 at 0:33 Haven't gotten into rails 3 yet :( – Tilendor Sep 21 '10 at 20:42 2 This works in Rails 3.

Modify config/application. Rb: add app/models/ to the autoload_paths - no namespace is required – tamersalama Nov 2 '10 at 18:52.

Here is what I used for Rails 3: config. Autoload_paths += DirRails.root. Join('app', 'models', '{**}') This configuration tells Rails to scan all the app/models subfolders recursively and load all found models.No namespacing required.

This version of Tilendor's solution works with Rails 3 config. Load_paths and RAILS_ROOT are deprecated in Rails 3, also you should put it in the config block of the config/application. Rb, not environment.

Rb config. Autoload_paths += Dir"#{Rails.root. To_s}/app/models/*".

Find_all { |f| File. Stat(f). Directory?

}.

A little cleaner is config. Autoload_paths += Dir"#{Rails.root. To_s}/app/models/**/" – pduey May 9 at 17:04 Thanks for the rails 3 update.

– Tilendor May 19 at 16:49.

Until I find a better solution I've created a init. Rb in the app/models folder: app/models/init. Rb %wblog.

Each do |folder| path = File. Dirname(__FILE__), folder, "*. Rb".

Join('/') Dirpath. Each {|file| require file } end Servers the purpose until now.

Maybe you could look upon RailsEngines. It's not exactly what you need, but could gave you some ideas. Other than that, if your script seems to work fine (you could also just read all the files on each subfolder on model and require them), I don't see any problem against it.

So I used to have in Rails 2 something like this: config. Autoload_paths += Dir"#{config. Root}/app/models/**/" And the following files: app/models/user/base.

Rb: class User::Base app/models/user/admin. Rb: class User::Admin When I upgraded to Rails 3, I kept getting an error along these lines: Expected .../app/models/user/foo. Rb to define Foo.

This clearly seemed crazy since Rails 2 automatically assumed that what you put in user/foo. Rb would be User::Foo not just Foo. So the way I ended up solving this was getting rid of model subdirectories in autoload_paths and doing something like this: I created app/models/user.

Rb with: module User autoload :User, 'user/base' autoload :User, 'user/admin' end.

I don't think that you can change this behaviour.

When I upgraded to Rails 3, I kept getting an error along these lines: Expected .../app/models/user/foo. Rb to define Foo. This clearly seemed crazy since Rails 2 automatically assumed that what you put in user/foo.

Rb would be User::Foo not just Foo. I created app/models/user.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions