Skip to content

Instantly share code, notes, and snippets.

@twnaing
Created December 15, 2021 05:11
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 twnaing/4a56e226321c390fce36da4ec928a2bb to your computer and use it in GitHub Desktop.
Save twnaing/4a56e226321c390fce36da4ec928a2bb to your computer and use it in GitHub Desktop.
Rails: Get the model class name based on the controller class name
class HouseBuyersController < ApplicationController
def index
@model_name = controller_name.classify
# Equivalent of @house_buyers = HouseBuyer.find(:all)
objects = @model_name.constantize.find(:all)
instance_variable_set("@#{controller_name}", objects)
end
end
@twnaing
Copy link
Author

twnaing commented Dec 15, 2021

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