# Misgivings (or misconceptions?) about class=table concept

**URL:** <https://www.ruby-forum.com/t/misgivings-or-misconceptions-about-class-table-concept/168425>\
**Category:** Rails\
**Created:** [May 20, 2009, 6:53pm UTC](https://www.ruby-forum.com/t/misgivings-or-misconceptions-about-class-table-concept/168425 "2009-05-20T18:53:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 20, 2009, 6:53pm UTC](https://www.ruby-forum.com/t/misgivings-or-misconceptions-about-class-table-concept/168425/1 "2009-05-20T18:53:47Z")

</div>

Please bear with me as I am totally new to Rails AND sql.

Let’s say a want a database & application for a record collection (using  
sql and rails). If I want the database to contain information down to  
the length of a song, how can I implement this using the class = table  
model?

For example, the way I have the db now, ignoring rails, is a table “of  
Contents” listing all artists (one column). Each artist then has a  
table listing all their albums, several columns since we want the total  
length of the album and (since this is a digital catalog) a location  
where the actual album is stored. Then we need a table for each album  
listing each song, with the length.

Going with the class=table model, that means I will be stuck with a  
separate class for every single artist and album, which is beyond  
ridiculous if not completely unworkable. But if I organize the material  
differently – eg, a table “artist” with a column “albums” (fine, if  
they were just objects) some single table cell would have to contain a  
list of all the albums. Slightly awkward, since each album will then  
have to be parsed out of a string of albums, but more realistic. Then I  
would have a table “albums” with columns/methods “artist” and “songs”.  
But when it comes down to including the length of each song, this  
methodology is gonna get dunder-headed.

The former model makes much more sense anyway, from any perspective  
_except_ it would appear to be unworkable with rails. Clearly I am  
missing something here, I hope. Anyone want to offer me a clue?

---

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 20, 2009, 7:43pm UTC](https://www.ruby-forum.com/t/misgivings-or-misconceptions-about-class-table-concept/168425/2 "2009-05-20T19:43:09Z")

</div>

On May 20, 12:53 pm, Mk 27 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Please bear with me as I am totally new to Rails AND sql.
> 
> Let’s say a want a database & application for a record collection (using  
> sql and rails). If I want the database to contain information down to  
> the length of a song, how can I implement this using the class = table  
> model?

Very easily. Read on.

> For example, the way I have the db now, ignoring rails, is a table “of  
> Contents” listing all artists (one column). Each artist then has a  
> table listing all their albums,

So you’re saying you have

table: Contents  
±------------------+  
| name |  
±------------------+  
| Michael J. |  
| Bruce Springsteen |

table: Michael J.  
±---------+  
| album |  
±---------+  
| Thriller |

table: Bruce Springsteen  
±-------------------+  
| album |  
±-------------------+  
| Working on a Dream |

?

If that’s what you’re saying, then this is poor database design, with  
or without Rails.

> several columns since we want the total  
> length of the album and (since this is a digital catalog) a location  
> where the actual album is stored. Then we need a table for each album  
> listing each song, with the length.

No we don’t. Keep reading.

> Going with the class=table model, that means I will be stuck with a  
> separate class for every single artist and album, which is beyond  
> ridiculous if not completely unworkable. But if I organize the material  
> differently – eg, a table “artist” with a column “albums” (fine, if  
> they were just objects) some single table cell would have to contain a  
> list of all the albums. Slightly awkward, since each album will then  
> have to be parsed out of a string of albums, but more realistic.

Still no. Please learn about the proper use of foreign keys and the  
Third Normal Form.

> Then I  
> would have a table “albums” with columns/methods “artist” and “songs”.  
> But when it comes down to including the length of each song, this  
> methodology is gonna get dunder-headed.
> 
> The former model makes much more sense anyway, from any perspective  
> _except_ it would appear to be unworkable with rails. Clearly I am  
> missing something here, I hope. Anyone want to offer me a clue?

The former model actually make _no_ sense, since it contains lots of  
tables with identical structure. That’s your cue to realize that they  
should be one table. What you want instead is something like this:

table: artists  
±—±------------------+  
| id | name |  
±—±------------------+  
| 1 | Michael J. |  
| 2 | Bruce Springsteen |

table: albums  
±—±-------------------±----------+  
| id | name | artist\_id |  
±—±-------------------±----------+  
| 1 | Working on a Dream | 2 |  
| 2 | Thriller | 1 |

table: songs  
±—±-------------±---------+  
| id | name | album\_id |  
±—±-------------±---------+  
| 1 | Beat It | 2 |  
| 2 | Thriller | 2 |  
| 3 | Outlaw Pete | 1 |  
| 4 | My Lucky Day | 1 |

Then use has\_many and belongs\_to properly and Rails will deal with  
creating the object graph.

Does that make more sense?

## Best,

Marnen Laibow-Koser  
removed\_email\_address@domain.invalid  
[http://www.marnen.org](http://www.marnen.org)

---

<div class="post-metadata">

**Author:** ![mk27](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/5e9695/32.png) [@mk27](https://www.ruby-forum.com/u/mk27)\
**Post date:** [May 20, 2009, 7:58pm UTC](https://www.ruby-forum.com/t/misgivings-or-misconceptions-about-class-table-concept/168425/3 "2009-05-20T19:58:49Z")

</div>

Marnen Laibow-Koser wrote:

> Does that make more sense?

Yep. Thanks much.
