# User specific data in subdomains

**URL:** <https://www.ruby-forum.com/t/user-specific-data-in-subdomains/119963>\
**Category:** Rails\
**Created:** [November 12, 2007, 4:02pm UTC](https://www.ruby-forum.com/t/user-specific-data-in-subdomains/119963 "2007-11-12T16:02:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 12, 2007, 4:02pm UTC](https://www.ruby-forum.com/t/user-specific-data-in-subdomains/119963/1 "2007-11-12T16:02:56Z")

</div>

Hi,

I have an app with specific data to each user. I want user _george_ to  
be able to access his data at [george.domain.com](http://george.domain.com), user _dan_ to access  
his data at [dan.domain.com](http://dan.domain.com) and so on. So I’d have a Users controller  
and I suppose I’d have to map [domain.com/users/show/george](http://domain.com/users/show/george) to  
[george.domain.com](http://george.domain.com). How can I do this? Do I use Request Routing plugin?  
Though, how do I do it with this plugin also?

Thanks ;).

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 12, 2007, 9:00pm UTC](https://www.ruby-forum.com/t/user-specific-data-in-subdomains/119963/2 "2007-11-12T21:00:40Z")

</div>

On Nov 12, 2007, at 7:01 AM, removed\_email\_address@domain.invalid wrote:

> I have an app with specific data to each user. I want user _george_ to  
> be able to access his data at [george.domain.com](http://george.domain.com), user _dan_ to access  
> his data at [dan.domain.com](http://dan.domain.com) and so on. So I’d have a Users controller  
> and I suppose I’d have to map [domain.com/users/show/george](http://domain.com/users/show/george) to  
> [george.domain.com](http://george.domain.com). How can I do this? Do I use Request Routing plugin?  
> Though, how do I do it with this plugin also?

Here’s how I do it:

I create a class which contains all the logic needed to dictate the  
behaviors of a subdomain. Let’s call this class Subdomain (usually it  
would be an ActiveRecord class). This class will typically just  
initialize a bunch of instance vars used as settings that make a  
Subdomain unique.

One of those settings is going to be one or more query filters to  
restrict data access. These are just SQL fragments (or :conditions  
hashes) which will modify the WHERE clause of all queries to restrict  
the query to the conditions suitable to the subdomain. So, in your  
case we might have “AND subdomain\_name = ‘george’” as the query filter.

class Subdomain

attr\_reader :site\_name, :query\_filter

def initialize  
@query\_filter  
end

def load\_settings  
… does a query to get all settings…  
… load results into instance vars…  
end

end

Then early in application.rb, you would create an object something  
like this:

@current\_subdomain = Subdomain.new  
@current\_subdomain.load\_settings(self.request.subdomains.join(‘.’))

You may or may not need the join part (I do state and county  
subdomains for some stuff, and that will join a multiple subdomin  
list into a single string).

Now for all the queries which collect data, you can use the value(s)  
in @current\_subdomain.query\_filter to add to the :conditions or  
modify an SQL string to restrict the query to specific records  
pertaining to the subdomain.

– gw (www.railsdev.ws)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 13, 2007, 1:07am UTC](https://www.ruby-forum.com/t/user-specific-data-in-subdomains/119963/3 "2007-11-13T01:07:47Z")

</div>

On Nov 12, 2007, at 1:29 PM, removed\_email\_address@domain.invalid wrote:

> Greg, thanks for your reply. I understand how to work with this, but  
> how do I set the usage of subdomains in routes.rb? do I need to do  
> anything special? you didn’t mention if you use any plugin for this?

There’s nothing to do in routes. No plugins. It’s just simple  
application logic that populates an object by reading the subdomain  
from the HTTP headers – which is what request.subdomains gives you.  
Then using you own object’s instance vars where you need them. No  
magic needed.

– gw (www.railsdev.ws)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 13, 2007, 4:43pm UTC](https://www.ruby-forum.com/t/user-specific-data-in-subdomains/119963/4 "2007-11-13T16:43:41Z")

</div>

Hi Greg,

I din’t get how one can map request URL from  
[domain.com/users/show/george](http://domain.com/users/show/george)  
To  
[george.domain.com](http://george.domain.com)

which indirectly means that when the user in the browser types  
“[domain.com/users/show/george](http://domain.com/users/show/george)”, how i can  
take that request to “[george.domain.com](http://george.domain.com)”. And one more thing is  
since “george” is a dynamic value, i am not able  
to understand how to do it.

Please make me understand,

Thanks,

Regards,  
Raghu Kumar K

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 12, 2007, 10:29pm UTC](https://www.ruby-forum.com/t/user-specific-data-in-subdomains/119963/5 "2007-11-12T22:29:47Z")

</div>

Greg, thanks for your reply. I understand how to work with this, but  
how do I set the usage of subdomains in routes.rb? do I need to do  
anything special? you didn’t mention if you use any plugin for this?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 14, 2007, 9:25am UTC](https://www.ruby-forum.com/t/user-specific-data-in-subdomains/119963/6 "2007-11-14T09:25:23Z")

</div>

On Nov 13, 2007, at 7:37 AM, raghukumar wrote:

> I din’t get how one can map request URL from  
> [domain.com/users/show/george](http://domain.com/users/show/george)  
> To  
> [george.domain.com](http://george.domain.com)  
> which indirectly means that when the user in the browser types  
> “[domain.com/users/show/george](http://domain.com/users/show/george)”, how i can  
> take that request to “[george.domain.com](http://george.domain.com)”.  
> And one more thing is since “george” is a dynamic value, i am not able  
> to understand how to do it.

I went ahead and wrote up an article on all this:

[http://www.railsdev.ws/blog/10/using-subdomains-in-rails-apps/](http://www.railsdev.ws/blog/10/using-subdomains-in-rails-apps/)

– gw (www.railsdev.ws)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [November 13, 2007, 8:59pm UTC](https://www.ruby-forum.com/t/user-specific-data-in-subdomains/119963/7 "2007-11-13T20:59:29Z")

</div>

> > Then using you own object’s instance vars where you need them. No  
> > magic needed.

On Nov 13, 2007, at 7:37 AM, raghukumar wrote:

> I din’t get how one can map request URL from  
> [domain.com/users/show/george](http://domain.com/users/show/george)  
> To  
> [george.domain.com](http://george.domain.com)  
> which indirectly means that when the user in the browser types  
> “[domain.com/users/show/george](http://domain.com/users/show/george)”, how i can  
> take that request to “[george.domain.com](http://george.domain.com)”.

No, that wasn’t the original question. We’re not trying convert URLs  
from one format to another. The original question was how to use the  
explicit URLs of [george.domain.com](http://george.domain.com), [dan.domain.com](http://dan.domain.com), [mary.domain.com](http://mary.domain.com),  
and any \*.domain.com and have Rails figure out what to do based on  
that (which turns out to be quite simple).

> And one more thing is since “george” is a dynamic value, i am not able  
> to understand how to do it.

Simply configure your HTTP server to accept any subdomain. In Apache  
this would be:

ServerName [domain.com](http://domain.com)  
ServerAlias \*.domain.com

Now Apache will accept every subdomain.

If someone submits [nonsense.domain.com](http://nonsense.domain.com) and you have no data for  
“nonsense,” you serve a default page that tells the user that. It’s  
just a “page not found” kind of thing.

– gw (www.railsdev.ws)
