Skip to content

Instantly share code, notes, and snippets.

@hakanensari
Created September 6, 2009 22:18
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 hakanensari/182017 to your computer and use it in GitHub Desktop.
Save hakanensari/182017 to your computer and use it in GitHub Desktop.
# Following works with PostgreSQL when I want to SELECT DISTINCT
# and ORDER BY a field but use another if the first is NULL.
has_many :things,
:through => :hashtags,
:select => "DISTINCT things.*, CASE WHEN things.completed_at IS NOT NULL THEN things.completed_at ELSE things.created_at END AS created_or_completed_at",
:order => "created_or_completed_at DESC"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment