How access controller method in models

Posted by : on

Category : Rails

How ever in practice some time we require to access the controller’s methods in the models.It’s quite easy to do so in rails. Here is an explanation of that:

Add a before_filter method inside the Application Controller

Add a before_filter method inside the application controller and write the following command code inside that function

Thread.current.thread_variable_set('controller', self)

Access inside the model

Now you can access the controller’s method inside the model by using

Thread.current.thread_variable_get('controller').method_name

Note

You have to make your method_name public to avoid it calling from browser as action have to hide it by

hide_action :method_name


About Ram Laxman Yadav
Ram Laxman Yadav

Senior Software Engineering Professional | Tech Enthusiast | Mentor | Payments | Hospitality | E-Commerce, based in NCR, India

Email : info@ramlaxman.co.in

Website : https://ramlaxman.co.in