Skip to content

Instantly share code, notes, and snippets.

@0xjmp
Created January 24, 2014 18:52
Show Gist options
  • Save 0xjmp/8603642 to your computer and use it in GitHub Desktop.
Save 0xjmp/8603642 to your computer and use it in GitHub Desktop.
class Relationship < ActiveRecord::Base
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"
validates :follower_id, presence: true
validates :followed_id, presence: true
end
class User < ActiveRecord::Base
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :followed_users, through: :relationships, source: :followed
has_many :reverse_relationships, foreign_key: "followed_id",
class_name: "Relationship",
dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment