Rails best way to get previous and next active record object

后端 未结 4 1356
野的像风
野的像风 2020-12-03 14:57

I need to get the previous and next active record objects with Rails. I did it, but don\'t know if it\'s the right way to do that.

What I\'ve got:

Controller

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-03 15:09

    There's no easy out-of-the-box solution.

    A little dirty, but working way is carefully sorting out what conditions are there for finding next and previous items. With id it's quite easy, since all ids are different, and Rails Guy's answer describes just that: in next for a known id pick a first entry with a larger id (if results are ordered by id, as per defaults). More than that - his answer hints to place next and previous into the model class. Do so.

    If there are multiple order criteria, things get complicated. Say, we have a set of rows sorted by group parameter first (which can possibly have equal values on different rows) and then by id (which id different everywhere, guaranteed). Results are ordered by group and then by id (both ascending), so we can possibly encounter two situations of getting the next element, it's the first from the list that has elements, that (so many that):

    • have the same group and a larger id
    • have a larger group

    Same with previous element: you need the last one from the list

    • have the same group and a smaller id
    • have a smaller group

    Those fetch all next and previous entries respectively. If you need only one, use Rails' first and last (as suggested by Rails Guy) or limit(1) (and be wary of the asc/desc ordering).

提交回复
热议问题