Skip to content

Instantly share code, notes, and snippets.

@listrophy
Created March 6, 2013 21:43
Show Gist options
  • Save listrophy/5103363 to your computer and use it in GitHub Desktop.
Save listrophy/5103363 to your computer and use it in GitHub Desktop.
GitHub-style URL matching, with only one query. Is this belt-and-suspenders if ActiveRecord caches the query?
class ProjectLookupConstraint
def matches? request
Project.where(request.params.slice(:account_name, :project_name)).first.tap do |project|
request.flash.now['project'] = project
end
end
end
class ProjectsController < ApplicationController
expose(:project) { flash.now['project'].decorate }
end
require 'project_lookup_constraint'
MyApp::Application.routes.draw do
get ':account_name/:project_name' => 'projects#show', constraints: ProjectLookupConstraint.new
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment