Skip to content

Instantly share code, notes, and snippets.

@evanrmurphy
Created December 2, 2010 05:12
Show Gist options
  • Save evanrmurphy/724806 to your computer and use it in GitHub Desktop.
Save evanrmurphy/724806 to your computer and use it in GitHub Desktop.
class Followship < ActiveRecord::Base
attr_accessible :followed_id
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 :followships, :foreign_key => "follower_id",
:dependent => :destroy
has_many :following, :through => :followships,
:source => :followed
def following?(followed)
followships.find_by_followed_id(followed)
end
def follow!(followed)
unless following? followed
followships.create!(:followed_id => followed.id)
end
end
def unfollow!(followed)
followships.find_by_followed_id(followed).destroy
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment