Namespacing models in a Rails application
Asked Answered
M

1

7

I had a discussion recently with a friend of mine who is also a RoR developer. We argued about how Rails models should be managed. Personally I like to leave in the default namespace only the root models (e.g. User, Article, Bill etc.), and the dependent models go to a module (e.g. User::Profile, User::Activity) with the name of the root model they are associated with.

On the other hand, I've seen many projects which had like 100 models in the default namespace called like user_profile, user_activity and so on. Judging by Java (Spring) development, java community tends to organize class in packages and have them grouped logically, which I find very appealing.

So the question is: are there any drawback in grouping models in modules (except the extra :class_name in relation definition) and are there any specific reasons why people usually don't do it?

Musa answered 28/3, 2012 at 18:30 Comment(0)
A
4

Although namespacing has its advantages, it does require adding exceptions throughout your models. Foo::Bar presumes a table name of bars and likewise bar_id for associations, whereas you might prefer foo_bars and foo_bar_id to be used instead.

If you really feel strongly about this, you might want to see if there's an add-on that fixes this for you, or implement your own extension that does.

The only case when I've used namespaces is for add-ons that are to be used in third party applications where I don't want to claim root-level model names as that would be annoying. The extra effort in this case is worth-while.

If you're bothered by seeing 100+ model files without any grouping, you'll probably be equally annoyed by seeing 100+ tables with no grouping, and that's generally something you can't fix.

Controllers lend themselves to grouping quite naturally, but models aren't as easily accommodated, at least not with stock ActiveRecord.

Anselma answered 28/3, 2012 at 19:6 Comment(2)
I can't remember the last time I opened the database explorer. With ruby it comes naturally to abstract such things. As far as I remember, Foo::Bar will generate a "foo_bar" table. The only drawback is that you have to specify a :class_name => Foo::Bar in associations. I wonder how a rails app is supposed to handle a complex application if it encourages to keep all models in one directory. I mean, if the app grows too much, is namespacing a part of the needed refactoring or one just switches to Java?)Musa
I usually take the approach of using the prefix as a sort of namespace, so you create things like UserProfile and UserActivity instead of Profile or Activity. This leads to a natural sort of grouping in the listing. It's rare to see an application with 500+ models that would benefit significantly from namespacing. Complicated is complicated. Sometimes it's better to be honest about how complex your application is than to try and pretend it's actually simple by burying the complexity and just making it harder to find things.Anselma

© 2022 - 2024 — McMap. All rights reserved.