Skip to content

Instantly share code, notes, and snippets.

@czottmann
Created April 30, 2009 17:07
Show Gist options
  • Save czottmann/104553 to your computer and use it in GitHub Desktop.
Save czottmann/104553 to your computer and use it in GitHub Desktop.
class CreateRelationships < ActiveRecord::Migration
def self.up
create_table :relationships do |t|
t.integer :user_id
t.integer :following_id
t.timestamps
end
add_index( :relationships, [ :user_id, :following_id ], :unique => true )
end
def self.down
drop_table :relationships
end
end
class Relationship < ActiveRecord::Base
belongs_to :favourite_user, :class_name => "User", :foreign_key => "following_id"
belongs_to :follower, :class_name => "User", :foreign_key => "user_id"
end
class User < ActiveRecord::Base
has_many :relationships
has_many :favourite_users, :through => :relationships
has_many :followers, :through => :relationships
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment