Rails: How to get the model class name based on the controller class name?

前端 未结 6 2074
别那么骄傲
别那么骄傲 2020-12-22 20:49
class HouseBuyersController < ...
  def my_method
    # How could I get here the relevant model name, i.e. \"HouseBuyer\" ?
  end
end
6条回答
  •  渐次进展
    2020-12-22 20:55

    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
    

提交回复
热议问题