Rails best way to get previous and next active record object

你离开我真会死。 提交于 2019-12-17 11:43:38

问题


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:

@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

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

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.

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.

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


回答1:


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




回答2:


Write these methods in your Product model:

class Product

  def next
    self.class.where("id > ?", id).first
  end

  def previous
    self.class.where("id < ?", id).last
  end

end

Now you can do in your controller:

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

@previous_product = @product.next
@next_product = @product.previous

Please try it, but its not tested. Thanks




回答3:


I think it would be faster to do it with only two SQL requests, that only select two rows (and not the entire table). Considering that your default order is sorted by id (otherwise, force the sorting by id) :

@previous_product = Product.where('id < ?', params[:id]).last
@next_product = Product.where('id > ?', params[:id]).first

If the product is the last, then @next_product will be nil, and if it is the first, then, @previous_product will be nil.




回答4:


This is what order_query does. Please try the latest version, I can help if it doesn't work for you:

class Product < ActiveRecord::Base
  order_query :my_order,
    [:product_sub_group_id, :asc], 
    [:id, :asc]     
  default_scope -> { my_order } 
end

@product.my_order(@collection.products).next
@collection.products.my_order_at(@product).next

This runs one query loading only the next record. Read more on Github.



来源:https://stackoverflow.com/questions/25665804/rails-best-way-to-get-previous-and-next-active-record-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!