Guest User

Untitled

a guest
Feb 21st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. create_table :friendships do |t|
  2. t.column :user_id, :integer, :null => false
  3. t.column :friend_id, :integer, :null => false
  4. t.column :status, :integer, :null => false, :default => 0
  5. t.column :created_at, :datetime
  6. end
  7.  
  8. class User < ActiveRecord::Base
  9. has_many :friendships_as_user,
  10. :foreign_key => 'user_id',
  11. :class_name => 'Friendship'
  12.  
  13. has_many :friendships_as_friend,
  14. :foreign_key => 'friend_id',
  15. :class_name => 'Friendship'
  16.  
  17. has_many :users,
  18. :through => :friendships_as_user,
  19. :source => :friend
  20.  
  21. has_many :friends_from_me,
  22. :through => :friendships_as_user,
  23. :source => :user,
  24. :conditions => 'friendships.status = 1'
  25.  
  26. has_many :friends_for_me,
  27. :through => :friendships_as_friend,
  28. :source => :friend,
  29. :conditions => 'friendships.status = 1'
  30.  
  31. has_many :awaiting_friends_from_me,
  32. :through => :friendships_as_user,
  33. :source => :user,
  34. :conditions => 'friendships.status = 0'
  35.  
  36. has_many :awaiting_friends_for_me,
  37. :through => :friendships_as_friend,
  38. :source => :friend,
  39. :conditions => 'friendships.status = 0'
  40.  
  41. def friends
  42. self.friends_for_me + self.friends_from_me
  43. end
  44.  
  45. def awaiting_friends
  46. self.awaiting_friends_for_me + self.awaiting_friends_from_me
  47. end
  48. end
  49.  
  50. class Friendship < ActiveRecord::Base
  51. belongs_to :user, :class_name => 'User', :foreign_key => :friend_id
  52. belongs_to :friend, :class_name => 'User', :foreign_key => :user_id
  53. end
Add Comment
Please, Sign In to add comment