103
class HouseBuyersController < ...
  def my_method
    # How could I get here the relevant model name, i.e. "HouseBuyer" ?
  end
end
Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746

6 Answers6

195

This will do it:

class HouseBuyersController < ApplicationController

  def index
    @model_name = controller_name.classify
  end

end

This is often needed when abstracting controller actions:

class HouseBuyersController < ApplicationController

  def index
    # Equivalent of @house_buyers = HouseBuyer.find(:all)
    objects = controller_name.classify.constantize.find(:all)
    instance_variable_set("@#{controller_name}", objects)
  end

end
malclocke
  • 5,192
  • 4
  • 20
  • 9
  • @model_name = controller_name.classify # returns "HouseBuyers" thanks! – Peter Ehrlich Jan 28 '12 at 19:36
  • Depending on your needs, (this question doesn't specify any details), you can do `controller_name.sub('_', ' ').titleize` to get "House Buyers". – user664833 Feb 19 '12 at 03:31
  • It might be good to move the logic (for getting the model class) into a separate utility method. Even better if the result is stored in a constant, to speed up access. You can use `new.controller_name.classify.constantize` in the class definition. – Kelvin Jun 07 '13 at 17:56
  • I think it's worth noting that this will even work in the application controller depending on which controller inherits from it. – Dennis Hackethal Dec 16 '13 at 22:09
  • this solution doesn't work with namespaces. See my solution below. – hachpai Mar 26 '15 at 19:39
46

If your controller and model are in the same namespace, then what you want is

controller_path.classify

controller_path gives you the namespace; controller_name doesn't.

For example, if your controller is

Admin::RolesController

then:

controller_path.classify # "Admin::Role" # CORRECT
controller_name.classify # "Role"        # INCORRECT
Matt
  • 20,108
  • 1
  • 57
  • 70
6

It's a bit of a hack, but if your model is named after your controller name then:

class HouseBuyersController < ApplicationController
  def my_method
    @model_name = self.class.name.sub("Controller", "").singularize
  end
end

... would give you "HouseBuyer" in your @model_name instance variable.

Again, this makes a huge assumption that "HouseBuyersController" only deals with "HouseBuyer" models.

Scott
  • 17,127
  • 5
  • 53
  • 64
4

For namespaces working:

def resource_class
 controller_path.classify.constantize
end
hachpai
  • 196
  • 12
0

The accepted solution did not work for me as my controller and model was namespaced. Instead, I came up with the following method:

def controllers_model
  (self.class.name.split('::')[0..-2] << controller_name.classify).join('::')
end
stoffus
  • 1
  • 1
-1

This is not possible if you are using the default MVC, which your code doesn't seem to follow. Your controller seems to be a model but maybe you just got a type there. Anyway, controllers and models are fundamentally separated in Rails MVC so controllers cannot know which model they are associated with.

For example you could have a model named post. This can have a controller posts_controller or could have a controller like articles_controller. Rails only knows about models when you def the actual code in the controller such as

def index
  @posts = Post.all
  @posts = Article.all
end  

In rails standard controllers there is no way to know what the model is.

thenengah
  • 42,557
  • 33
  • 113
  • 157