Searching from a range of ids in ActiveRecord

前端 未结 4 1753
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-17 07:30

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 spec

相关标签:
4条回答
  • 2021-01-17 08:00

    You can do it like this too:

    User.find_by_id(14000..14500)
    
    0 讨论(0)
  • 2021-01-17 08:01

    Use the where method:

    User.where(id: 14000..14500)
    
    0 讨论(0)
  • 2021-01-17 08:07

    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})
    
    0 讨论(0)
  • 2021-01-17 08:08

    Try this also

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

    Ex -

    User.find((14000..14500).to_a)
    
    0 讨论(0)
提交回复
热议问题