0

I want to run a query on my User model, like this:

@user = User.find(:all, :conditions=>["u_org != c_org and u_org == ?", current_web.role.id])

This is the older style for running queries and it is not working as well. Can you please help me to run it in rails 3. I have tried:

@user = User.where("u_org != c_org and u_org == ?", current_web.role.id).all

and the result was [].

Update:

I have removed == as per the answer and still the query returns []. I have tried only with @user = User.find(:all, :conditions=>["u_org = ?", current_web.role.id]) and the value is returned successfully but i want it to be @user = User.find(:all, :conditions=>["u_org != c_org and u_org = ?", current_web.role.id])

Edit:

Sample Data:

u_org = 1

c_org = null

current_web.role.id = 1

2
  • You have tried and...? There was an error, it didn't return expected result? You need to be more specific. Commented Aug 6, 2013 at 18:58
  • The first query would not work as well, see vinodadhikary answer. Commented Aug 6, 2013 at 19:03

2 Answers 2

1

It looks fine, the only problem is your use of double equals ==. Try the not working well query as follows:

@user = User.find(:all, :conditions=>["u_org != c_org and u_org = ?", current_web.role.id])

And similarly your second as follows:

@user = User.where("u_org != c_org and u_org = ?", current_web.role.id).all
Sign up to request clarification or add additional context in comments.

5 Comments

It is still the same. Nothing happens.
If you're getting [] in return then the query executed without any errors. You need to check your data. You could start by having only one condition that returns records and then apply another condition.
I have tried with @user = User.find(:all, :conditions=>["u_org = ?", 2]) and it returns the value, but when i use u_org!=c_org it shows [] and looking at database table there is 2 under u_org and null under c_org. Is the null doing something?
In SQL, you check for NULL using IS NULL or IS NOT NULL. The != and = operators do not work for NULL.
@sushilthe, that query should work just fine. Please, post some sample data in your question.
0

I think the problem is because of NULL, if you want all the users with c_org not equal to null use c_org IS NOT NULL and for c_org equalt to null use c_org IS NULL

@user = User.find(:all, :conditions=>["c_org IS NOT NULL and u_org = ?", current_web.role.id])

Edited

@user = User.find(:all, :conditions=>["(u_org != c_org OR c_org IS NULL) and u_org == ?", current_web.role.id])

2 Comments

I want u_org != c_org but the value of c_org is null
@sushilthe:- Check my Edited answer, i guess you want users whose having c_org is null or users who having c_org not same as u_org

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.