Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4: organize rails models in sub path without namespacing models?

Would it be possible to have something like this?

app/models/ app/models/users/user.rb app/models/users/education.rb 

The goal is to organize the /app/models folder better, but without having to namespace the models.

An unanswered question for Rails 3 is here: Rails 3.2.9 and models in subfolders.

Specifying table_name with namespaces seems to work (see Rails 4 model subfolder), but I want to do this without a namespace.

like image 519
Rubytastic Avatar asked Sep 21 '13 15:09

Rubytastic


People also ask

What is a namespace in rails?

Namespaces are one of the simplest and most powerful tools developers have to keep a growing domain model organized, but without a specific formula for how to apply this tool, Rails developers will find themselves having to make a number of decisions along the way, and the consequences of those decisions aren't always clear when you're running r...

Do you have to tell rails the class name of a table?

Outside the namespace, though, you’ll have to tell Rails the class name of any association that uses your namespaced model. Otherwise, it will guess incorrectly: Since most databases don’t support namespaces for tables, Rails expects you to prefix your table names with the “underscored” name of your namespace.

What is the hierarchy of the resource controller namespaces?

The hierarchy of namespaces for the resource controllers will typically match the hierarchy of the namespaces for the models themselves. For example, the Surveys::Answer might be managed from a Surveys::AnswersController or Api::V1::Surveys::AnswersController.

What models should not be in their own namespace?

Don't put models that represent the subject of a namespace in their own namespace. Say we know we're going to have a subscriptions subsystem in our application. We all know that subsystem is going to have a bunch of related models, (e.g. Plan ,) and it would make sense to put those models in a Subscriptions:: namespace.


1 Answers

By default, Rails doesn't add subfolders of the models directory to the autoload path. Which is why it can only find namespaced models -- the namespace illuminates the subdirectory to look in.

To add all subfolders of app/models to the autoload path, add the following to config/application.rb:

config.autoload_paths += Dir[Rails.root.join("app", "models", "{*/}")] 

Or, if you have a more complex app/models directory, the above method of globing together all subfolders of app/models may not work properly. In which case, you can get around this by being a little more explicit and only adding the subfolders that you specify:

config.autoload_paths += Rails.root.join("app", "models", "<my_subfolder_name1>") config.autoload_paths += Rails.root.join("app", "models", "<my_subfolder_name2>") 

UPDATE for Rails 4.1+

As of Rails 4.1, the app generator doesn't include config.autoload_paths by default. So, note that the above really does belong in config/application.rb.

UPDATE

Fixed autoload path examples in the above code to use {*/} instead of {**}. Be sure to read muichkine's comment for details on this.

like image 91
pdobb Avatar answered Sep 21 '22 17:09

pdobb