Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 763911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T16:38:11+00:00 2026-05-14T16:38:11+00:00

I’m trying to set up a new project for a music site. I’m learning

  • 0

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 :users
    drop_table :playlists
    drop_table :songs
    drop_table :comments
    drop_table :users_playlists
    drop_table :users_comments
    drop_table :playlists_comments
    drop_table :songs_comments
  end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T16:38:12+00:00Added an answer on May 14, 2026 at 4:38 pm

    Few things:

    Short version:

    1. Join tables should be alphabetical (e.g. create_table :comments_users
    2. This is only one half of setting up your associations. I’d like to see your model code as well
    3. Your comments model should be setup to be a polymorphic association to the others.

    Long version:

    1. When setting up a join table (e.g. users and comments) you want to make it alphabetical. So that the users/comments table is actually create_table :comments_users. Ideally, you would want to setup a more meaningful name for this join table (since it is a model – see below) but it’s your call.
    2. Remember that you have to create models for these :has_many through associations. Most of these join tables could be created with :has_and_belongs_to_many but for future expansions, may as well use the :has_many through method. It would be wise to provide your association code in your model files as well – just as important as the creation of tables.
    3. For comments, think about polymorphic relationships. This is because the comments are the same across all the different tables it belongs to. What this does is look for a type column in the polymorphic join table and that tells it what table the record (i.e. comment) belongs to.

    Starting Resources:

    Check out Ryan’s brilliant (as usual) Railscast on the subject:
    http://railscasts.com/episodes/47-two-many-to-many

    Also, on polymorphism:
    http://railscasts.com/episodes/154-polymorphic-association

    http://guides.rubyonrails.org/association_basics.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.