Is this the correct way to set up has many with multiple associations?

Posted by user323763 on Stack Overflow See other posts from Stack Overflow or by user323763
Published on 2010-04-22T22:51:04Z Indexed on 2010/04/22 22:53 UTC
Read the original article Hit count: 160

Filed under:

I'm trying to set up a new project for a music site. I'm learning ROR and am a bit confused about how to make join models/tables. Does this look right?

I have users, playlists, songs, and comments. Users can have multiple playlists. Users can have multiple comments on their profile. Playlists can have multiple songs. Playlists can have comments. Songs can have comments.

class CreateTables < ActiveRecord::Migration
  def self.up

    create_table :users do |t|
      t.string :login
      t.string :email
      t.string :firstname
      t.string :lastname
      t.timestamps
    end

    create_table :playlists do |t|
      t.string :title
      t.text :description
      t.timestamps
    end

    create_table :songs do |t|
      t.string :title
      t.string :artist
      t.string :album
      t.integer :duration
      t.string :image
      t.string :source
      t.timestamps
    end

    create_table :comments do |t|
      t.string :title
      t.text :body
      t.timestamps
    end

    create_table :users_playlists do |t|
      t.integer :user_id
      t.integer :playlist_id
      t.timestamps
    end    

    create_table :playlists_songs do |t|
      t.integer :playlist_id
      t.integer :song_id
      t.integer :position
      t.timestamps
    end

    create_table :users_comments do |t|
      t.integer :user_id
      t.integer :comment_id
      t.timestamps
    end

    create_table :playlists_comments do |t|
      t.integer :playlist_id
      t.integer :comment_id
      t.timestamps
    end

    create_table :songs_comments do |t|
      t.integer :song_id
      t.integer :comment_id
      t.timestamps
    end

  end

  def self.down
    drop_table :playlists
    drop_table :comments
    drop_table :songs_comments
    drop_table :users_comments
    drop_table :users_playlists
    drop_table :users
    drop_table :playlists
    drop_table :songs
    drop_table :playlists
  end
end

© Stack Overflow or respective owner

Related posts about ruby-on-rails