Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Migration, Friendship and Inverse Friendship


  • Please log in to reply
No replies to this topic

#1 rubyonrails

rubyonrails

    Passenger

  • Members
  • 1 posts

Posted 13 December 2016 - 03:59 AM

I am not sure how to do a proper migration. I am guessing I don't have to tie user_id and friend_id to anything during the migration and this will be done in the models instead. Is this alright?

class CreateFriendList < ActiveRecord::Migration
    def up
        create_table :friendship do |t|
            t.column "user_id", :integer, :limit=>255
            t.integer "friend_id", :limit=>255
            t.timestamps
        end
    end
    def down
        drop_table :friendship
    end

end

Again, do I need to specify that user_id and friend_id are foreign keys of user during the migration or is this alright?

User
    has_many :friendships
    has_many :friends, :through => :friendships
Friendship
    belongs_to :user
    belongs_to :friend, :class_name => "User"

Also, for inverse_friendship, do we do the exact same db migration and just change the name friendship to inverse_friendship?






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users