Help with domain model/database design


#1

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


#2

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


#3

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


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

http://www.alien8recordings.com


#4

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