Skip to content

Instantly share code, notes, and snippets.

@d3r1v3d
Created March 18, 2011 03:34
Show Gist options
  • Save d3r1v3d/875577 to your computer and use it in GitHub Desktop.
Save d3r1v3d/875577 to your computer and use it in GitHub Desktop.
class CarsController < ApplicationController
# ...
# GET /cars/1
def show
@car = Car.find(params[:id])
# ...
end
# GET /cars/1/edit
def edit
@car = Car.find(params[:id])
# ...
end
# ... and so on ...
end
def initialize_vars_from_param_ids
params.keys.each do |param_key|
model_klass_name = case param_key
when 'id' then
self.class.name.gsub(/Controller$/, '').singularize
else
next unless param_key =~ /(.+)_id$/
$1.singularize.camelize
end
model_klass = model_klass_name.constantize rescue next
self.instance_variable_set(
"@#{model_klass_name.tableize.singularize}",
model_klass.find(params[param_key])
) if model_klass.respond_to?(:find)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment