I tried adding followers/following to emulate friends for a web app I’m making in RoR. Any existing users before the change can still sign in. Registering new users is impossible now. Pushing the submit button on Devise’s sign up page just says the fields can’t be blank.
Everything marked by + is what I added to the model. Anything else I’ve added has just been views, rails generated test files, and a Relationships model/controller.
class User < ActiveRecord::Base # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable has_many :gifts, :dependent => :destroy + has_many :relationships, :dependent => :destroy, + :foreign_key => "follower_id" + has_many :reverse_relationships, :dependent => :destroy, + :foreign_key => "followed_id", + :class_name => "Relationship" + has_many :following, :through => :relationships, :source => :followed + has_many :followers, :through => :reverse_relationships, + :source => :follower + def following?(followed) + relationships.find_by_followed_id(followed) + end + def follow!(followed) + relationships.create!(:followed_id => followed.id ) + end + def unfollow!(followed) + relationships.find_by_followed_id(followed).destroy + end end