From b3640e078c365af0f6dee7bf2c725debc6091ad3 Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Mon, 27 Oct 2014 12:27:32 -0400 Subject: [PATCH] Disallow self reference friend --- app/models/friendship.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/models/friendship.rb b/app/models/friendship.rb index bc7f6aa..0abf718 100644 --- a/app/models/friendship.rb +++ b/app/models/friendship.rb @@ -3,4 +3,11 @@ class Friendship < ActiveRecord::Base belongs_to :friend, class_name: 'User' validates_uniqueness_of :friend_id, :scope => :user_id + validate :disallow_self_reference + + def disallow_self_reference + if friend_id == user_id + errors.add(:friend_id, 'cannot refer back to self') + end + end end