Created
October 7, 2008 10:19
-
-
Save collin/15265 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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