Skip to content

Instantly share code, notes, and snippets.

@User195
Last active December 31, 2015 02:28
Show Gist options
  • Save User195/7920565 to your computer and use it in GitHub Desktop.
Save User195/7920565 to your computer and use it in GitHub Desktop.
class User < ActiveRecord::Base
.
.
.
.
.
.
.
.
has_many :microposts, dependent: :destroy
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
.
.
.
.
def following?(other_user)
relationships.find_by_followed_id(other_user.id)
end
def follow!(other_user)
relationships.create!(followed_id: other_user.id)
end
def unfollow!(other_user)
relationships.find_by_followed_id(other_user.id).destroy
end
.
.
.
.
.
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment