Skip to content

Instantly share code, notes, and snippets.

@squiter
Created February 5, 2014 21:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save squiter/8833478 to your computer and use it in GitHub Desktop.
Save squiter/8833478 to your computer and use it in GitHub Desktop.
Followers in Rails
class Followings < ActiveRecord::Base
attr_accessible :user_id, :follower_id
belongs_to :user
belongs_to :follower, class_name: 'User'
belongs_to :following, class_name: 'User', foreign_key: :user_id
validates_uniqueness_of :user_id, scope: :follower_id
end
class Users < ActiveRecord::Base
has_many :match_followers, class_name: 'Followings', foreign_key: 'user_id'
has_many :match_following, class_name: 'Followings', foreign_key: 'follower_id'
has_many :followers, through: :match_followers
has_many :following, through: :match_following
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment