RailsApps/rails3-subdomains — Issue #1: More on models "restricted" by the subdomain
Author: eimermusic
Created: 2011-12-06 07:39:36+00:00
State: open

I wanted to make something like a "feature request".

This app is a great example because of Mongoid and Device but only goes about as far as other tutorials on subdomains I have seen. I.E. creating a profile page based on the subdomain. What I have never seen covered is a more generally useful technique for partitioning data to subdomains. You may call it "Basecamp-style" or however you want to call it.

Basically a re-usable way to make sure that **sub1.domain.com/posts** only show posts created for that subdomain. (Dumb classic example, I know.)

In some apps, scoping the user and going on associations from there is enough but I think in many cases you want more.

I have run into problems trying to make Mongoid do this with any degree of Magic®. It would seem easy to use the default scope to say limit a **domain** field to **request.subdomain**. Doesn't work. Mongoid "resolves" the default scope only once and then re-uses that for every query in every request. So, after a restart, all queries would scope to the domain of the first request. (Not apparent while in development)

The reason I think subdomain constraints should be implemented sort-of like default_scope is that it becomes a configuration on the model and not something you can forget to do in some query somewhere. This feels a lot safer when the object is to avoid data bleeding between domains.

Thanks for a nice example app.