Skip to content

Instantly share code, notes, and snippets.

@benhamill
Created May 17, 2011 18:48
Show Gist options
  • Save benhamill/977095 to your computer and use it in GitHub Desktop.
Save benhamill/977095 to your computer and use it in GitHub Desktop.
Two ways that take a case statement out of your controller.
class UserConroller
def index
@users = User.sort_by(params[:sort_by])
end
end
class User
def sort_by(sort_by)
case sort_by
:created_at order(:created_at)
:last_updated order(:last_updated)
else order(:id)
end
end
end
class UserController
def index
@users = user.order(params[:sort_by] || :id)
end
end
@benhamill
Copy link
Author

Yeah. I may have fucked up the case syntax. I never use it and was too lazy to look it up. Hopefully it's still clear what the heck I'm blathering about. Your exclusivity point is well made.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment