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.
this is how i think the structure should be:
Code:class Album < ActiveRecord::Base has_one :artist, :through => tracks has_many :genres, :through => tracks has_many :tracks end class Artist < ActiveRecord::Base has_many :albums has_many :genres, :through => tracks has_many :tracks end class Genre < ActiveRecord::Base has_many :albums, :through => tracks has_many :artists, :through => tracks has_many :tracks end class Track < ActiveRecord::Base has_many :albums belongs_to :artist has_many :genres end
this is how i imagine my tables to look:
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.Code:# albums table create_table :albums do |t| t.string :title t.integer :track_id t.integer :artist_id end # artists table create_table :artists do |t| t.string :title t.integer :track_id end # genres table create_table :genres do |t| t.string :title t.integer :track_id end # tracks table create_table :tracks do |t| t.string :title t.integer :number t.integer :genre t.datetime :duration t.timestamps end
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?
thanks.






Bookmarks