Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- create_table :friendships do |t|
- t.column :user_id, :integer, :null => false
- t.column :friend_id, :integer, :null => false
- t.column :status, :integer, :null => false, :default => 0
- t.column :created_at, :datetime
- end
- class User < ActiveRecord::Base
- has_many :friendships_as_user,
- :foreign_key => 'user_id',
- :class_name => 'Friendship'
- has_many :friendships_as_friend,
- :foreign_key => 'friend_id',
- :class_name => 'Friendship'
- has_many :users,
- :through => :friendships_as_user,
- :source => :friend
- has_many :friends_from_me,
- :through => :friendships_as_user,
- :source => :user,
- :conditions => 'friendships.status = 1'
- has_many :friends_for_me,
- :through => :friendships_as_friend,
- :source => :friend,
- :conditions => 'friendships.status = 1'
- has_many :awaiting_friends_from_me,
- :through => :friendships_as_user,
- :source => :user,
- :conditions => 'friendships.status = 0'
- has_many :awaiting_friends_for_me,
- :through => :friendships_as_friend,
- :source => :friend,
- :conditions => 'friendships.status = 0'
- def friends
- self.friends_for_me + self.friends_from_me
- end
- def awaiting_friends
- self.awaiting_friends_for_me + self.awaiting_friends_from_me
- end
- end
- class Friendship < ActiveRecord::Base
- belongs_to :user, :class_name => 'User', :foreign_key => :friend_id
- belongs_to :friend, :class_name => 'User', :foreign_key => :user_id
- end
Add Comment
Please, Sign In to add comment