Skip to content

Instantly share code, notes, and snippets.

@JunichiIto
Created April 7, 2014 05:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JunichiIto/10015280 to your computer and use it in GitHub Desktop.
Save JunichiIto/10015280 to your computer and use it in GitHub Desktop.
Sample code to find next/prev user by date_of_birth.
user = User.find params[:id]
older_user = User.where("date_of_birth <= ? AND id <> ?", user.date_of_birth, user.id).order("date_of_birth DESC, id").limit(1).first
younger_user = User.where("date_of_birth >= ? AND id <> ?", user.date_of_birth, user.id).order("date_of_birth ASC, id").limit(1).first
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment