Forum: Ruby on Rails Help with domain model/database design

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sean O'Hara (Guest)
on 2006-05-14 04:30
(Received via mailing list)
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
j`ey (Guest)
on 2006-05-14 11:07
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
Alien8 Recordings (Guest)
on 2006-05-14 18:01
(Received via mailing list)
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
Sean O'Hara (Guest)
on 2006-05-14 19:33
(Received via mailing list)
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
This topic is locked and can not be replied to.