1

Here's my code:

- unless Relationship.are_friends?(current_user, @user) && (current_user != @user)
  - puts "d"*100
  - puts "#{current_user.inspect}"
  - puts "#{@user.inspect}"
  = link_to "Add Friend", controller: "relationships", action: "req", name: @user.name,      remote: true, class: "friend_link"

Basically the second condition "current_user != @user" is not working and I don't know why. When I puts the current user and @user they are working correctly. @user = User.find_by_name(params[:name]).

Thanks for the help!

2
  • Is that how your code is actually indented? Assuming that's haml, nothing there is actually part of the unless statement. Commented Oct 7, 2013 at 2:34
  • oh yeah, sorry its .slim Commented Oct 7, 2013 at 2:39

2 Answers 2

1

I assume this is your requirement. Allow A to add B as a friend if:

  • A and B are not already friends AND
  • A and B are not the same person

So, in pseudo-code, unless (already_friends or same_person) allow_add_friend end

If this is your requirement, your statement should read:

unless Relationship.are_friends?(current_user, @user) or (current_user == @user)

Note that && changed to or and != changed to ==.

Sign up to request clarification or add additional context in comments.

Comments

1

unless is often a bit counterintuitive, especially with multiple conditions. Look at it this way:

true && false # => false, code is executed
false && true # => false, code is executed

The only condition in which the block is skipped is:

true && true  # => true, code is not executed

I imagine what you're trying to do is skip the block is either condition is true, in which case you need to use the || operator:

unless Relationship.are_friends?(current_user, @user) || (current_user == @user)
  #etc..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.