Rails - Where (directories) to put Models that are not Active Record
In my experience, the division of where you put these models comes down to what they functionally represent in the specific context of your application.
I usually reserve app/models
for resource based models. If that model represents a resource that is instantiated and manipulated by your application it goes here. Doesn't need to be AR or db backed.
If the model serves a consistent functionality but varies on the parameters, I give them a top level dir in app. Such as app/mailers
app/observers
etc. However, if you have one resource that requires an observer, it might not make sense to have an app/observers
dir with just one file in it.
Everything else goes in lib
. There are a few reasons why this is preferable.
You can choose when to require the files in
lib
. You can be much more selective about which files get loaded when your app starts. If you put everything inapp/models
you've got no granularity over what gets loaded.Namespacing your models as your app grows is easier in lib. Sure you can namespace in
app/models
but several layers of nesting inapp/models
always ends up nasty. It's best to keep the namespacing inlib
.Housekeeping is made much easier when you've got things in their functionally correct place. It's not a resource? It's not an observer? Must be in
lib
. The whole reason you're putting thought into this up front is to provide discoverability to developers down the line.
For service objects you'll usually have them directly under the app directory app/services/
. Workers and serializers also follow this pattern app/workers/
app/serializers/
. As for your models that are not AR you can still stick them in the models directory. That's just my take on it.
If they are models, you should put them into app/models
since this directory is meant for models and not just ActiveRecord subclasses.