Searching from a range of ids in ActiveRecord

2020-04-01 08:53发布

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.

4条回答
欢心
2楼-- · 2020-04-01 08:58

Use the where method:

User.where(id: 14000..14500)
查看更多
The star\"
3楼-- · 2020-04-01 09:05

You can use range definition for scoped:

User.find(1)       # returns the object for ID = 1
User.find([1])

User.find(1, 2, 9) # returns an array for objects with IDs in (1, 2, 9)
User.find([1, 2, 9])

User.scoped(:conditions => { :id => 1..9})
查看更多
走好不送
4楼-- · 2020-04-01 09:13

You can do it like this too:

User.find_by_id(14000..14500)
查看更多
Summer. ? 凉城
5楼-- · 2020-04-01 09:15

Try this also

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

Ex -

User.find((14000..14500).to_a)
查看更多
登录 后发表回答