in Ruby on Rails

On Scope, Lambdas, & SQL Queries

Adding MMR (match making ranking) filtering to Dotawithme has been an oft requested feature on Reddit so I finally decided to tackle the issue. I wasn’t exactly sure the best way to go about it but in the end I decided I wanted to put rankings in a range of 500. I thought the best way to do this was to use a dropdown menu with a variety of ranges to select from (ie: 1001-1500 or 1501-2000).

I wanted to filter the results by using scope just as I do for the other parameters. Except I was having a hard time using a range to list results by. I found one way where you could enter a range like this (1..10) or (1001..1500). This appeared to work so I wrote a bunch of if/elsif statements for the different ranges based on the input. However the code wouldn’t appear to go past my first if statement and would only list for that range; regardless of the input. This led me to lambda (which is now simply displayed as -> instead of the word lambda).

Unfortunately I couldn’t find a range function that would work inside the lambda function. I solved this by simply writing the query statement myself.

In my model I have this. This asks for a list based on players.solommr between the parameter I pass it (solommr) and solommr+500 since this is the range I wanted. The code to_in ensures it is an integer. Then in my view I have the following:

This is a simple dropdown menu. As you can see it lets you select for a different range and then I pass the bottom end of that range to get the listing (which is why I add 500 to the second variable in the model.

Hopefully this will help someone that is trying to use scope to sort listings by a range of numbers.

Write a Comment

Comment