# Scoped databases need explanation

**URL:** <https://www.ruby-forum.com/t/scoped-databases-need-explanation/53972>\
**Category:** Rails\
**Created:** [February 18, 2006, 9:27pm UTC](https://www.ruby-forum.com/t/scoped-databases-need-explanation/53972 "2006-02-18T21:27:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [February 18, 2006, 9:27pm UTC](https://www.ruby-forum.com/t/scoped-databases-need-explanation/53972/1 "2006-02-18T21:27:21Z")

</div>

Could someone explain this

[http://blog.leetsoft.com/articles/2005/10/31/scoped-databases](http://blog.leetsoft.com/articles/2005/10/31/scoped-databases)

in a way that can be understood with an example if possible. Tobi Lutke  
has started to explain but it does not make any sense.

“Since rails 0.13.1 we support calling class methods over associations.”  
what?? give an example.

also

“The Shop object is figured out at the beginning of each request by  
looking at the incoming domain of the user.” has no example of how it  
could be used.

I feel this is a very important aspect to rails that needs some futher  
explanation.

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [February 18, 2006, 10:22pm UTC](https://www.ruby-forum.com/t/scoped-databases-need-explanation/53972/2 "2006-02-18T22:22:44Z")

</div>

On Feb 18, 2006, at 12:27 PM, James W. wrote:

> what?? give an example.
> 
> also
> 
> “The Shop object is figured out at the beginning of each request by  
> looking at the incoming domain of the user.” has no example of how it  
> could be used.
> 
> I feel this is a very important aspect to rails that needs some futher  
> explanation.

```
What they are probably doing is getting the shop in a before_filter

```

using the subdomain. Something like this:

class ApplicationController \< ActionController::Base

```
before_filter :setup_shop

attr_accessor:shop

protected

def setup_shop
  if @shop = Shop.find_by_subdomain(request.subdomains.first)
  else
    @shop = Shop.find_by_subdomain("default")
  end
end

```

end

This way on every request @shop will be set the the correct shop  
according to the subdomain in the url like:

[http://fooshop.example.com](http://fooshop.example.com)

So @shop will be the current shop account on every request and if the  
request doesn’t have a subdomain it gets set tho the default shop.  
Then you can scope your queries like in the blog article:

class Product \< AR:B  
def self.search(q)  
find(:all, :conditions =\> “title LIKE ‘%#{q}%’”)  
end  
end

@shop.products.search(â??snowboardsâ?) will run this find query:

Product.find(:all, :conditions =\> [“title LIKE ? AND shop\_id = ?”, “%#  
{q}%”, 2]

The shop\_id = 2 is added automatically by these scoped queries. So in  
the above @shop.id would be 2 and the Product search is scoped to  
that particular shop.

Hope that makes sense to you 😉

Cheers-

-Ezra Z.  
WebMaster  
Yakima Herald-Republic Newspaper  
removed\_email\_address@domain.invalid  
509-577-7732

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [February 19, 2006, 3:01pm UTC](https://www.ruby-forum.com/t/scoped-databases-need-explanation/53972/3 "2006-02-19T15:01:35Z")

</div>

That is brilliant and now makes sense to me thanks.

Just one thing, how can I set the subdomains up for each new account on  
the system. Is that done within rails or is it a server setup?

Basically I have a table called “customers” and for each new customer I  
would like to map their name to a subdomain just like the other hosted  
apps out there.

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [February 19, 2006, 9:44pm UTC](https://www.ruby-forum.com/t/scoped-databases-need-explanation/53972/4 "2006-02-19T21:44:03Z")

</div>

On Feb 19, 2006, at 6:01 AM, James W. wrote:

> 

James-

```
Well you need to configure your web server and dns to accept any

```

subdomain. And then you can just store the customers subdomain in the  
Customer model and use the technique I just showed you. There are  
also some good helpers for using subdomains and redirecting to them  
here:

module AccountLocation  
def self.included(controller)  
controller.helper\_method  
(:account\_domain, :account\_host, :account\_url)  
end

protected  
def default\_account\_subdomain  
@account.company\_abbrev if @account && @account.respond\_to?  
(:company\_abbrev)  
end

```
 def account_url(account_subdomain = default_account_subdomain,

```

use\_ssl = request.ssl?)  
(use\_ssl ? “https://” : “http://”) + account\_host  
(account\_subdomain)  
end

```
 def account_host(account_subdomain = default_account_subdomain)
   account_host = ""
   account_host << account_subdomain + "."
   account_host << account_domain
 end

 def account_domain
   account_domain = ""
   account_domain << request.subdomains[1..-1].join(".") + "." if

```

request.subdomains.size \> 1  
account\_domain \<\< request.domain + request.port\_string  
end  
end

```
Inlcude that in your application controller like so:

```

class ApplicationController \< ActionController::Base  
include AccountLocation

Cheers-

-Ezra Z.  
WebMaster  
Yakima Herald-Republic Newspaper  
removed\_email\_address@domain.invalid  
509-577-7732
