Forum: Ruby on Rails Misgivings (or misconceptions?) about class=table concept

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.
68814ed988eee4f21b2cd07353e5e445?d=identicon&s=25 Mk 27 (mk27)
on 2009-05-20 18:53
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?
Dd2d775dea75b381edb1bbf0600a0907?d=identicon&s=25 Marnen Laibow-Koser (marnen)
on 2009-05-20 19:43
(Received via mailing list)
On May 20, 12:53 pm, Mk 27 <rails-mailing-l...@andreas-s.net> 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 Jackson   |
| Bruce Springsteen |

table: Michael Jackson
+----------+
| 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 Jackson   |
| 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
marnen@marnen.org
http://www.marnen.org
68814ed988eee4f21b2cd07353e5e445?d=identicon&s=25 Mk 27 (mk27)
on 2009-05-20 19:58
Marnen Laibow-Koser wrote:

> Does that make more sense?

Yep.  Thanks much.
This topic is locked and can not be replied to.