0

In my rails app I need to find all where:

balance != nil or 0
pause != true

I tried this but it didn't work

@foo = Product.all
@foo = @foo.where( "pause != ?", true )
@foo = @foo.where( "balance != ?", 0 )
@foo = @foo.where( "balance != ?", nil )
2
  • When you say it doesn't work, what do you get? An error or an unexpected output? Commented Jan 4, 2014 at 2:31
  • i get an empty object Commented Jan 4, 2014 at 2:32

1 Answer 1

2

I believe you are ending up with an implicit AND between all conditions. How about something like

@foo = Product.where("pause!=? AND (balance !=? OR balance is not null)", true, 0)

Edit: updated to check for is not null instead of comparison with null. Refer to the Active Record query interface here

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

2 Comments

@muistooshort - didn't realize that. What would you recommend for null checks?
balance is not null, x = null and x != null are both false for all x.

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.