轨获得previous和下一个活动记录对象最好的方式 [英] Rails best way to get previous and next active record object

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

问题描述

我需要得到previous和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)}

所以,现在的问题是,我需要去我的数据库,并得到这一切的IDS知道谁是previous而下。

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.

试图用宝石 order_query ,但它并没有为我工作,我注意到,它去到数据库中的所有记录读取的顺序,所以,这就是为什么我做的一样,但只得到了IDS。

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.

所有我找到的解决很简单为了querys。订单来自序号或类似一个优先级字段的东西。

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

推荐答案

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

一个有点脏,但工作方式是经过精心整理出什么条件都没有寻找下一个和previous项目。随着 ID 这是很容易的,因为所有的 ID 是不同的,的 Rails的家伙的回答说明这一点:在接下来为已知 ID 挑选具有较大的 ID 第一项(如果结果是由 ID ,责令按默认值)。更重要的是 - 他的回答暗示放置下一步 previous 入模型类。这样做。

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.

如果有多阶标准,事情就变得复杂。再说了,我们有一组行排序的参数,第一个(可可能有不同的行相等的值),然后通过 ID (其中ID不同的无处不在,保证)。结果按 组责令 ,然后按 ID (无论),所以我们都不可能遇到获取下一个元素的两种情况,这是第一个从具有元素的列表(这么多的的):

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

  • 同一 ID
  • 有无较大

同样的,previous元素:从列表中所需的最后一个

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

  • 同一 较小 ID
  • 有无较小

这些获取所有下一步和previous项分别。如果你只需要一个,使用Rails的第一最后(所建议的Rails盖伊)或者限制(1)(和警惕的递增/递减排序)。

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

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

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