Created
October 2, 2018 17:14
-
-
Save kevinluo201/df5e373e667ee7d19dbcd1038bb78e87 to your computer and use it in GitHub Desktop.
next, prev
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User | |
has_many :photos | |
end | |
class Photo | |
belongs_to :user | |
def next | |
user.photos.where("id > ?", id).first | |
end | |
def prev | |
user.photos.where("id < ?", id).last | |
end | |
end | |
Now you can: | |
photo.next | |
photo.prev |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment