开发者

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

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


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


That looks ok. You seem to have the ID's in all the right place.

Couple of points though:

It's often easier to have many small migrations. I generally create each model and migration individually. You can then start building your model relationships in lock-step with your tests to ensure that your business requirements are actually being implemented.

You have some tests, right? :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜