Rails: Get next / previous record

前端 未结 9 1039
说谎
说谎 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:16

    You can pass some options into the where method:

    For the next photo:

    Photo.where(:user_id => current_user.id, :created_at > current_photo.created_at).order("created_at").first
    

    Previous photo

    Photo.where(:user_id => current_user.id, :created_at < current_photo.created_at).order("created_at").last
    

    I may have the first/last mixed up.

提交回复
热议问题