The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Hybrid View

inducting songs into a db using has_many :through

Hi,
I'm building an app where it reads a dir for files that end with .mp3 and insert them into a db along with the id3 tags in the files.

my problem is the relationship between the tables. i dont fully understand "has many through" associations yet. i come from a background of 5 years in PHP and i'm only just starting to learn ROR...i know a decent amount of Ruby though.

there's a rule that a track or album can only have one artist. if an album happens to have many artists, it will go as 'Various Artists' and if a track has a featured artist for example, it would be a part of the track title.

is this correct? any suggestions would be great too.
also, what if a track doesn't belong to an album, how would i go about sorting that out?

Has many through relationships are used when just a simple join table doesn't represent the entire relationship.

A basic many-to-many relationhip may be as follows:
users can be in many groups
groups can have many users
join table is users_groups, that simply maps users to multiple groups.

A more advanced relationship appropriate for has many through could be as follows:
magazines have many readers through subscriptions
readers are associated to magazines through subscriptions

Rather than just a simple magazines_readers join table, you would have a table called subscriptions which not only mapped magazines to readers, but also contained data such as subscription start and end date, price, etc.

To summarize, you use has_many :through when the relationship between two tables contains more data than simply that each record can have many of the other.

That being said, it is still ok (and probably the standard now) to use has_many through even if its just a simple join relationship, because it gives you the ability to create a third model with a name that makes sense in your business logic. A has_and_belongs_to_many won't have that third model.

jeffct, i agree with you.
i was going to use has_and_belongs_to_many but everybody else told me to use has_many through...almost as if they expect has_and_belongs_to_many to be deprecated in the future.

however, my question is will my model work? and is it efficient for what i've described?