Two columns must not equal each other in Rails
Try it like this:
class Friendship < ActiveRecord::Base
validate :cannot_add_self
private
def cannot_add_self
errors.add(:user2_id, 'You cannot add yourself as a friend.') if user1_id == user2_id
end
end
if :user1_id == :user2_id
That's always gonna be false - you're comparing the symbols. It's the same as writing if "user1_id" == "user2_id"
.
You should write it as if user1_id == user2_id
to compare the values of the columns.