Skip to content

Instantly share code, notes, and snippets.

@timothyklim
Created April 3, 2011 08:31
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 timothyklim/900292 to your computer and use it in GitHub Desktop.
Save timothyklim/900292 to your computer and use it in GitHub Desktop.
application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
def method_missing(method, *args)
if method.to_s =~ /^find_([a-z]+)_id$/
find_id($1)
elsif method.to_s =~ /^find_([a-z]+)$/
find_id($1, :search_name_only)
else
super
end
end
private
def find_id(name, *args)
if args.include?(:search_name_only)
id = params[name]
else
id = params["#{name}_id"].presence || params[:id]
end
# Create instance variable like @university = University.find(1)
instance_eval "@#{name} = #{name.capitalize}.find(#{id})"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment