ruby - Rails - how to get the records by their rank -


i have city model holds names of cities , respective populations. how write method show cities range of ranks?

for example, i'd able pass in 2 numbers method high end of ranks , 1 low end. ie, pass in get_cities_by_pop(1,10) top 10 cities population, while get_cities_by_pop(20,35) cities ranked 20-35 population.

so far have:

def get_cities_by_pop(high, low)   cities = city.all.order('population desc') end 

but not sure how write logic.

you have need on activerecord querying guide page.

you can use limit specify number of records retrieved, , use offset specify number of records skip before starting return records. example

client.limit(5) 

will return maximum of 5 clients , because specifies no offset return first 5 in table. sql executes looks this:

select * clients limit 5 

adding offset that

client.limit(5).offset(30) 

will return instead maximum of 5 clients beginning 31st. sql looks like:

select * clients limit 5 offset 30 

in case, end result like:

client.order('population desc').offset(high-1).limit(low) 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -