2

I'm trying to do a rails scope using a lambda function by passing in one value:

"scope :for_rank, lambda {|rank| where('min_rank <= ? AND max_rank >= ?', rank, rank)}"

however, it is possible that max_rank is null. In this situation, I want the query to only do the min_rank <= ? part. How can I do this?

Example:

"a.min_rank = 10"
"a.max_rank = 20"
"b.min_rank = 15"
"b.max_rank = nil"

i want the search of for_rank(15) should return both a and b.

1 Answer 1

2

Try this:

 scope :for_rank, lambda {|rank| where('(min_rank <= ? AND max_rank IS NOT NULL AND max_rank >= ?) OR (min_rank <= ? AND max_rank IS NULL)', rank, rank, rank)}
Sign up to request clarification or add additional context in comments.

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.