Created
December 15, 2021 05:11
-
-
Save twnaing/4a56e226321c390fce36da4ec928a2bb to your computer and use it in GitHub Desktop.
Rails: Get the model class name based on the controller class name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://stackoverflow.com/a/6770284/188574