Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching from a range of ids in ActiveRecord

How can I do something like this in range?

User.find(14000..14500)

I need to choose a certain range of Users starting and finishing on specifics ids.

like image 649
Martin Avatar asked May 27 '12 10:05

Martin


3 Answers

You can use the where method:

User.where(id: 14000..14500)

Explanation

The where method here receives a hash argument in its shortened form, where the value for the id key is a Range.

like image 102
KARASZI István Avatar answered Nov 19 '22 19:11

KARASZI István


You can do it like this too:

User.find_by_id(14000..14500)
like image 21
blawzoo Avatar answered Nov 19 '22 21:11

blawzoo


Try this also

User.find((start..end).to_a)

Ex -

User.find((14000..14500).to_a)
like image 2
Arvind singh Avatar answered Nov 19 '22 20:11

Arvind singh