Active Record - Get the second, third.. item in a database (without ID)

后端 未结 3 1153
终归单人心
终归单人心 2021-01-01 20:45

How to I retrieve the second, third .. entries in a database. I don\'t want to use the auto incrementing id generated by rails.

As I am deleting entries from my data

相关标签:
3条回答
  • 2021-01-01 20:52

    Model.second was added to Rails 4.1.8.

    So you can use it on Rails versions equal to or greater than that.

    .third, .fourth and .fifth were also added to ActiveRecord::FinderMethods.

    0 讨论(0)
  • 2021-01-01 21:00

    what you need is

    #n=2,3,4 etc    
    .limit(1).offset(n) 
    
    0 讨论(0)
  • 2021-01-01 21:05

    Say you want the fourth user:

     @users = User.limit(4)
     @fourth_user = @users[3]
    

    Concerning your second question, destroy_all should do the trick since it triggers all callbacks. Be sure to add :dependent => :destroy in your relationships.

    0 讨论(0)
提交回复
热议问题