Misgivings (or misconceptions?) about class=table concept


#1

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?


#2

On May 20, 12:53 pm, Mk 27 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


#3

Marnen Laibow-Koser wrote:

Does that make more sense?

Yep. Thanks much.