Rails 获取上一个和下一个活动记录对象的最佳方法 [英] Rails best way to get previous and next active record object

查看:20
本文介绍了Rails 获取上一个和下一个活动记录对象的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 Rails 获取上一个和下一个活动记录对象.我做到了,但不知道这样做是否正确.

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.

我有什么:

控制器:

@product = Product.friendly.find(params[:id])

order_list = Product.select(:id).all.map(&:id)

current_position = order_list.index(@product.id)

@previous_product = @collection.products.find(order_list[current_position - 1]) if order_list[current_position - 1]
@next_product = @collection.products.find(order_list[current_position + 1]) if order_list[current_position + 1]

@previous_product ||= Product.last
@next_product ||= Product.first

product_model.rb

product_model.rb

default_scope -> {order(:product_sub_group_id => :asc, :id => :asc)}

所以,这里的问题是我需要去我的数据库并获取所有这些 ID 才能知道谁是前一个和下一个.

So, the problem here is that I need to go to my database and get all this ids to know who is the previous and the next.

尝试使用 gem order_query,但它对我不起作用,我注意到它进入数据库并获取所有记录都按这个顺序,所以,这就是为什么我做了同样的事情,但只得到了 id.

Tried to use the gem order_query, but it did not work for me and I noted that it goes to the database and fetch all the records in that order, so, that's why I did the same but getting only the ids.

我找到的所有解决方案都是使用简单的订单查询.按 id 或优先级字段之类的内容排序.

All the solutions that I found was with simple order querys. Order by id or something like a priority field.

推荐答案

没有简单的开箱即用的解决方案.

有点脏,但工作方式是仔细整理寻找下一个和上一个项目的条件.使用 id 很容易,因为所有 id 都是不同的,而且 Rails Guy'sanswer 仅描述:在 next 中,对于已知的 id,选择具有较大 id 的第一个条目(如果结果按以下顺序排序)id,默认情况下).不仅如此 - 他的回答提示将 nextprevious 放入模型类中.这样做.

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.

如果有多个订单条件,事情就会复杂化.比如说,我们有一组行首先按 group 参数排序(它可能在不同的行上具有相同的值)然后按 id (其 id 随处不同,保证).结果按groupid(均升序)排序,所以我们可能会遇到两种获取下一个元素的情况,它是列表中第一个有元素的元素,即 (so many that):

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):

  • 具有相同的group更大的id
  • 更大的group

与前一个元素相同:您需要列表中的最后一个

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

  • 具有相同的group较小的id
  • 一个较小的group

那些分别获取所有下一个和上一个条目.如果您只需要一个,请使用 Rails 的 firstlast(如 所建议的)Rails Guy) 或 limit(1)(并注意 asc/desc 排序).

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).

这篇关于Rails 获取上一个和下一个活动记录对象的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆