# Ecommerce site w/multiple subdomains

**URL:** <https://www.ruby-forum.com/t/ecommerce-site-w-multiple-subdomains/154578>\
**Category:** Rails\
**Created:** [December 1, 2008, 8:28pm UTC](https://www.ruby-forum.com/t/ecommerce-site-w-multiple-subdomains/154578 "2008-12-01T20:28:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![wemagor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8edcca/32.png) [@wemagor](https://www.ruby-forum.com/u/wemagor)\
**Post date:** [December 1, 2008, 8:28pm UTC](https://www.ruby-forum.com/t/ecommerce-site-w-multiple-subdomains/154578/1 "2008-12-01T20:28:43Z")

</div>

My company is looking at redoing our legacy Classic ASP storefront,  
and I’m thinking about doing it in Rails since the alternative is to  
use a PHP-based solution called Magento that, while it looks nice, is  
insanely abstract to the point of making me go blind (it uses a  
database design principle called Entity-Attribute-Value or EAV which  
basically is to keep database tables like hashmaps that can contain an  
infinite number of values).

The principle requirement of this site is to be able to filter certain  
products based on the subdomain, since we want to have different  
“brands”. The products aren’t specific to this subdomain, but the  
subdomain only shows a subset of products.

For example:

[www.mysite.com](http://www.mysite.com): displays all products with categories/subcategories  
(i.e. no filtering)  
[furniture.mysite.com](http://furniture.mysite.com): displays only products/subcategories with a  
parent category of furniture  
[green.mysite.com](http://green.mysite.com): displays only products/categories/subcategories with  
a (boolean) flag recycled = true

How would I go about doing this? The only way I can think would be to  
have separate controllers for each “store”, for example:

# Main store i.e. no subdomain

class StoreController \< ApplicationController  
def index  
@products = Products.find(:all)  
end  
end

# [furniture.mysite.com](http://furniture.mysite.com)

class FurnitureStoreController \< ApplicationController  
def index  
@products = Products.find\_all\_by\_category(‘Furniture’)  
end  
end

# [green.mysite.com](http://green.mysite.com)

class GreenStoreController \< ApplicationController  
def index  
@products = Products.find\_all\_by\_recycled(true)  
end  
end

But that seems rather unweildy. This is a critical business  
requirement since we want to create different stores with their own  
unique look and feel, but the actual data displayed comes from one  
master database and needs to be filtered appropriately based on the  
store the user is browsing.

Another critical requirement we have is that each individual product  
can have up to four prices, and the price is chosen based on the  
“store” the user is viewing it on (e.g. Product A is $15.00 on the  
[mysite.com](http://mysite.com), but $12.00 on [furniture.mysite.com](http://furniture.mysite.com)). I’m not sure what  
the best way to tackle that problem would be.

Any assistance with either or both of these issues would be greatly  
appreciated!

---

<div class="post-metadata">

**Author:** ![wemagor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8edcca/32.png) [@wemagor](https://www.ruby-forum.com/u/wemagor)\
**Post date:** [December 1, 2008, 9:05pm UTC](https://www.ruby-forum.com/t/ecommerce-site-w-multiple-subdomains/154578/2 "2008-12-01T21:05:13Z")

</div>

On Dec 1, 7:27 pm, Wayne M [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> But that seems rather unweildy. This is a critical business  
> requirement since we want to create different stores with their own  
> unique look and feel, but the actual data displayed comes from one  
> master database and needs to be filtered appropriately based on the  
> store the user is browsing.

Just the first thing that springs to mind.

in your product class do

named\_scope :green, …  
named\_scope :recycled, …

def self.scope\_for\_subdomain(subdomain)  
send case subdomain  
when ‘green’ then :green  
when ‘recycled’ then :recycled  
else  
:all  
end  
end

and then your index action is

Product.scope\_for\_subdomain(request.subdomains.first).find :all  
You could also just use anonymous scopes

Depending on what’s in your app it might be better to pull the  
selecting of the scope out into a before filter.

> Another critical requirement we have is that each individual product  
> can have up to four prices, and the price is chosen based on the  
> “store” the user is viewing it on (e.g. Product A is $15.00 on the  
> [mysite.com](http://mysite.com), but $12.00 on [furniture.mysite.com](http://furniture.mysite.com)). I’m not sure what  
> the best way to tackle that problem would be.

Again a random thought: products have a base price, and you have a  
price\_overrides table (products has\_many price\_overrides etc…)  
Assuming you have Store objects describing the stores and a filter  
that sets the current store you could have a method in your product  
class similar to

def overriden\_price(store)  
override = price\_overrides.find\_by\_store\_id store  
override ? override.price : self.price  
end

and when you want to display the price to a user you just do

product.overriden\_price(@current\_store)

Fred
