# Help with domain model/database design

**URL:** <https://www.ruby-forum.com/t/help-with-domain-model-database-design/60457>\
**Category:** Rails\
**Created:** [May 14, 2006, 2:30am UTC](https://www.ruby-forum.com/t/help-with-domain-model-database-design/60457 "2006-05-14T02:30:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sean\_OSHara](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@Sean\_OSHara](https://www.ruby-forum.com/u/Sean_OSHara)\
**Post date:** [May 14, 2006, 2:30am UTC](https://www.ruby-forum.com/t/help-with-domain-model-database-design/60457/1 "2006-05-14T02:30:03Z")

</div>

Hi All,

I was wondering if I could get some comments on my approach to the  
design of the domain model and database for my rails app. I don’t  
have much experience in this so I am wondering if I am heading in the  
right direction.

My app is an ecommerce site for my business which is a record label.  
That might seem straight forward, but the one thing that might be  
tricky is that we’ll be selling both hard and soft goods (e.g. CDs  
and MP3s). I want both types of goods to be able to be added to the  
same cart.

So here is my approach so far. I’ve created a class called Product  
which can contain a reference to one of either SoftProduct or  
HardProduct. These in turn refer/relate to a couple additional  
dependent classes, depending on whether we are dealing with a hard of  
soft good. So whether the Product contains a SoftProduct or a  
HardProduct, the cart mechanism remains relatively simple because it  
is just a matter adding a Product. All the logic of determining one  
kind of attributes a Product has, which is based on what kind of  
objects it contains, remains in the Product class.

Here’s a simple diagram:

* * *

| Product |  
| |  
| \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ |  
| | SoftProduct | | HardProduct | |  
| | | | | |  
| | \_\_\_\_\_\_\_\_\_\_\_\_\_\_ | | \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ | |  
| | | Album | | | | Option | | |  
| | |_**| | | |\_\_\_\_\_\_\_\_\_\_\_\_\_\_| | |  
| | | | | |  
| | or | | | |  
| | \_\_\_\_\_\_\_\_\_\_\_\_\_\_ | | | |  
| | | Song | | | | |  
| | |**_| | | | |  
| | | | | |  
| |_ **| |** __| |  
|_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_|

Is this the right way to go? Or does adding this child classes  
involve too much unnecessary overhead overhead in terms of extra sql  
queries, etc?

Thanks,  
Sean

---

<div class="post-metadata">

**Author:** ![Sean\_OSHara](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@Sean\_OSHara](https://www.ruby-forum.com/u/Sean_OSHara)\
**Post date:** [May 14, 2006, 9:07am UTC](https://www.ruby-forum.com/t/help-with-domain-model-database-design/60457/2 "2006-05-14T09:07:12Z")

</div>

Sean O’Hara wrote:

> Is this the right way to go? Or does adding this child classes  
> involve too much unnecessary overhead overhead in terms of extra sql  
> queries, etc?
> 
> Thanks,  
> Sean

If your doing this using single table inheritance, then this is  
definitely the way to go, as far as I know, there is little extra  
overhead from SQL.

j`ey  
[http://www.eachmapinject.com](http://www.eachmapinject.com)

---

<div class="post-metadata">

**Author:** ![Sean\_OSHara](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@Sean\_OSHara](https://www.ruby-forum.com/u/Sean_OSHara)\
**Post date:** [May 14, 2006, 4:01pm UTC](https://www.ruby-forum.com/t/help-with-domain-model-database-design/60457/3 "2006-05-14T16:01:51Z")

</div>

Hi J’ey,

I wasn’t going to do it with single table inheritance. I’m not sure  
that would work in this case, or at least I’m not sure how I would go  
about doing that. I assume that I would need separate table for each  
class because they all have different attributes that need to be  
saved in the db. Here are my models:

class Product \< ActiveRecord::Base  
belongs\_to :release  
has\_one :hard\_product, :dependent =\> :destroy  
has\_one :soft\_product, :dependent =\> :destroy  
end

class HardProduct \< ActiveRecord::Base  
belongs\_to :product  
end

class SoftProduct \< ActiveRecord::Base  
belongs\_to :product  
has\_one :track, :dependent =\> :destroy  
end

class Track \< ActiveRecord::Base  
belongs\_to :soft\_product  
end

Would it be possible to get this king of relationship with single  
table inheritance?

Thanks,  
Sean

On 14-May-06, at 3:07 AM, j`ey wrote:

> overhead from SQL.  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

* * *

ALIEN8 RECORDINGS  
P.O. BOX 666, STATION R  
MONTREAL, QC  
CANADA, H2S 3L1

[http://www.alien8recordings.com](http://www.alien8recordings.com)

---

<div class="post-metadata">

**Author:** ![Sean\_OSHara](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@Sean\_OSHara](https://www.ruby-forum.com/u/Sean_OSHara)\
**Post date:** [May 14, 2006, 5:33pm UTC](https://www.ruby-forum.com/t/help-with-domain-model-database-design/60457/4 "2006-05-14T17:33:01Z")

</div>

Hi,

So, I’ve just read up on STI and indeed that seems to be the way to  
go in this case. Thanks for pointing that out to me.

Sean
