I am working on a large(ERP level) Rails project. We have 150 tables and more than 150 models. It takes minutes to find a model. Should we add all models under the models folder or should we put them in different subfolders? Same thing goes for controllers and views.
2 Answers
I think you are running into the limitations of a single Rails application. Much like having too much code in one module, it sounds like you have too many modules in one application.
You should consider breaking it up into multiple applications using a service model, or you might also go the route of plugins or even better look at the new Engine framework.
This will benefit you multiple ways. It will force you to exercise good encapsulation and have good testable interface points between pieces.
Good Luck
-
You have described the situation very well. Thanks for that. Can you be more specific about multiple applications? How do I do that? Can you provide some links?– MetinJan 12, 2011 at 4:22
-
I think he is implying that some of your functionality can be broken off into other projects as a sort of web service style architecture. That would allow some functional encapsulation and you would have smaller chunks to digest at a given time.– RigMar 5, 2012 at 0:44
-
I don't think it is necessary.
Doing the namespace route you mention (for models under separate directories) is what i would do.
I would also analyze the current performance issue further. Just having 150 models/tables should not cause more than a fraction of a second to be able to find and use the model! Is it actually the db query itself that takes a long time (when not cached)? That can be addressed in many ways (splitting data, aggregating data, adding indexes, changing queries to be more performant, etc.
You may or may not need to do controllers and views under separate directories.
To access things arranged this way you just use ::
, e.g. if you put lots of models about bedrooms and attics and basements, etc. under a subdirectory 'buildings' and then want to access it in the controller, use ::
, e.g. attics = Buildings::Attic.all