Rails: Get next / previous record

前端 未结 9 969
说谎
说谎 2020-12-07 17:48

My app has Photos that belong to Users.

In a photo#show view I\'d like to show \"More from this user\", and show a next and previous photo from that user. I would be

相关标签:
9条回答
  • 2020-12-07 18:19

    Not sure if this is a change in Rails 3.2+, but instead of:

    model.where("id < ?", id).first
    

    for the previous. You have to do

    .where("id > ?", id).last
    

    It seems that the "order by" is wrong, so first give you the first record in the DB, because if you have 3 items lower than the current, [1,3,4], then the "first" is 1, but that last is the one you ware looking for. You could also apply a sort to after the where, but thats an extra step.

    0 讨论(0)
  • 2020-12-07 18:20

    It lead me to a solution for my problem as well. I was trying to make a next/prev for an item, no associations involved. ended up doing something like this in my model:

      def next
        Item.where("id > ?", id).order("id ASC").first || Item.first
      end
    
      def previous
        Item.where("id < ?", id).order("id DESC").first || Item.last
      end
    

    This way it loops around, from last item it goes to the first one and the other way around. I just call @item.next in my views afterwards.

    0 讨论(0)
  • 2020-12-07 18:24
    class Photo < ActiveRecord::Base
      belongs_to :user
      scope :next, lambda {|id| where("id > ?",id).order("id ASC") } # this is the default ordering for AR
      scope :previous, lambda {|id| where("id < ?",id).order("id DESC") }
    
      def next
        user.photos.next(self.id).first
      end
    
      def previous
        user.photos.previous(self.id).first
      end
    end
    

    Then you can:

    photo.previous
    photo.next
    
    0 讨论(0)
提交回复
热议问题