Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does not equal conditional

Tags:

I want to have a where clause with an equal and does not equal condition:

@user = User.where(:user_id => current_user.id, :author_id != current_user.id).nil? ? (render :something) : (render :somethingelse) 

The above does not work:

syntax error, unexpected ')', expecting tASSOC ...d, :user_id != current_user.id).nil? ? (render :index) : (re...

If I change the second condition from != to => it will work, however.

How do I have both conditions in one where clase? Thank you

like image 362
user715697 Avatar asked May 14 '11 18:05

user715697


People also ask

HOW DO YOU DO NOT equal in conditional formatting?

If you want to highlight the differences between two columns of data with conditional formatting you can do so with a simple formula that uses the" not equal to" operator (e.g. <>) and mixed references.

Does not equal in if statement?

The "does not equal" operatorIf they are not equal, it will output TRUE , and if they are equal, it will output FALSE . This is the exact opposite functionality of the equals sign (=), which will output TRUE if the values on either side of it are equal and FALSE if they are not.

Can you use != In Excel?

We can use the “Not equal to” comparison operator in Excel to check if two values are not equal to each other. In Excel, the symbol for not equal to is <>. When we check two values with the not equal to formula, our results will be Boolean values which are either True or False.


1 Answers

Here's how you would use Arel to generate the query "select * from users where user_id = ? and author_id != ?":

users = User.arel_table User.where(users[:user_id].      eq(current_user.id).and(            users[:author_id].not_eq(current_user.id))) 

Using Arel isn't as concise as using Hash conditions for simple conditions, but it's a lot more powerful!

Here's a link to the full list of predications (eq, not_eq, gt, lt, etc.) available with Arel.

like image 87
Tyler Rick Avatar answered Oct 06 '22 01:10

Tyler Rick