views:

2075

answers:

4

Hi all,

Since the beginning, Rails has had issues with namespaced models. As time went on, pretty much everybody gave up on using it. Myself included.

With Rails 2.3 out, I'd like an update on the situation. Specifics questions I have in mind are:

  • first off, is it good to go?
  • table naming, what rule to follow?
  • associations, how to declare them with the least verbosity? how to name foreign key columns?
  • auto-requiring, does it work if you put the model files in a subdir matching the namespace? or, how to name and where to place the files?
  • generation, does the model generator handles namespaces successfully and correctly?
  • generation, how about the scaffold generator, which includes controllers?
  • any incompatibilities/quirkinesses one should be aware of?
+2  A: 

We recently had a big debate about this inside our company. I think at the end of the day, we figured that if you can't namespace tables inside a database, it makes no sense to namespace the models. We settled on prefixing our models (User, UserAddress, UserEmailAddresses) and putting them into the users directory, then using:

config.load_paths << "#{RAILS_ROOT}/app/models/users"

to load the models. To control the verbosity in our models, we do this frequently:

has_many :addresses, :class_name => "UserAddress"

When generating, we create it as if there was no namespace (script/generate model UserAddress) then manually copy it to the user directory.

Shrug. I guess in the end all this really gives you is a cleaner directory structure, which is actually more trouble for a VIM user like me, but nice for TextMaters.

efalcao
Well really, as a TextMater I must tell you that I cmd+T to go to files, I seldom use the file browser. Anyway, that's moderately helpful, but are you saying you have tried namespacing in rails 2.3 and it's still SOL?
kch
I use something similar to cmd+T in VIM, and the nice thing about putting things in folders is that you can do "m/u/" and cmd+T will probably narrow down to models/users/, which is very helpful (most cmd+t users don't realize that slashes narrow the list by directory)
efalcao
In response to the rest of your question, we didn't try it in 2.3. In addition, I haven't seen anything in the 2.3 changes that specifically "green lights" namespaced models.
efalcao
"if you can't namespace tables inside a database, it makes no sense to namespace the models." Rails does let you have multiple logical databases (http://pullmonkey.com/2008/4/21/ruby-on-rails-multiple-database-connections/), so this is still a good reason to want namespaced models.
Walt Gordon Jones
A: 

I would still stay away from it. Anything gained (which I'm not sure what that would honestly be) would definitely be lost when you consider the hassle and loss of brevity and clarity in your code.

My latest app has 87 resources, and includes administrative features all over the place. I see no need for namespacing, IMHO.

Matt Darby
Well, some people work better with namespacing, some don't care. You can always find ways around it, hell, plenty of languages have none. Despite need/alternatives, it's a question that would be good having an answer to.
kch
This is a shortsighted blanket statement. Compelling reasons for namespaces aren't less about the quantity of models than their parity.
dasil003
+7  A: 

The best writeup I've seen on the issue is from Strictly Untyped. To my knowledge 2.3 hasn't resolved any issues, which means they're still unreliable.

Pesto
anything with lolcats in the title must be good. will check.
kch
+1  A: 

Here's a real-world problem where namespacing makes some sense. Perhaps. Imagine that you have an application. Different clients want different extensions. The main system works for everyone, so you want to enhance in client-specific ways. So namespace "Client1::Extended_application" and "Client2::Enhanced_differently". The core models and main tables are the same, but the two different clients get their specific tables, and a role based authentication service can check to see whether the entire namespace is accessible or denied...

While that's do-able by creating longer file names with client-prefixes, it doesn't look as nice as the file system layout of namespaces, and namespaces allow easier identification and isolation of client-specific changes.

JezC