Skip to content

Instantly share code, notes, and snippets.

@collin
Created October 7, 2008 10:19
Show Gist options
  • Save collin/15265 to your computer and use it in GitHub Desktop.
Save collin/15265 to your computer and use it in GitHub Desktop.
class FriendMigration < ActiveRecord::Migration
def self.up
create_table :friends do |t|
t.string :name, :null => false
t.string :email, :null => false, :unique => true
t.boolean :admin, :default => false
t.string :crypted_password
t.timestamps
end
end
def self.down
drop_table :friends
end
end
class Follow < ActiveRecord::Base
belongs_to :follower, :class_name => 'Friend'
belongs_to :following, :class_name => 'Friend'
def reciprocate
update_attribute :reciprocated, true
r = Follow.new :follower => following, :following => follower
r.reciprocated = true
r.save
end
def ignore
update_attribute :ignored, true
end
end
class Friend < ActiveRecord::Base
has_many :following_follows,
:class_name => 'Follow',
:foreign_key => 'follower_id'
has_many :followed_follows,
:class_name => 'Follow',
:foreign_key => 'following_id'
has_many :followers,
:through => :followed_follows
has_many :followings,
:through => :following_follows
has_many :friends,
:through => :following_follows,
:source => :following,
:conditions => {'follows.reciprocated' => true}
has_many :requests,
:through => :followed_follows,
:source => :following,
:conditions => {'follows.reciprocated' => false, 'follows.ignored' => false}
has_many :pendings,
:through => :following_follows,
:source => :follower,
:conditions => {'follows.reciprocated' => false, 'follows.ignored' => false}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment