# Using controller methods in rake tasks

**URL:** <https://www.ruby-forum.com/t/using-controller-methods-in-rake-tasks/147041>\
**Category:** Rails\
**Created:** [August 27, 2008, 10:18pm UTC](https://www.ruby-forum.com/t/using-controller-methods-in-rake-tasks/147041 "2008-08-27T22:18:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![corstar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee59a6/32.png) [@corstar](https://www.ruby-forum.com/u/corstar)\
**Post date:** [August 27, 2008, 10:18pm UTC](https://www.ruby-forum.com/t/using-controller-methods-in-rake-tasks/147041/1 "2008-08-27T22:18:19Z")

</div>

I am writing a rails task that needs to call a controller method. Does  
anyone know how I can do this?

Thanks

---

<div class="post-metadata">

**Author:** ![corstar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee59a6/32.png) [@corstar](https://www.ruby-forum.com/u/corstar)\
**Post date:** [August 27, 2008, 10:53pm UTC](https://www.ruby-forum.com/t/using-controller-methods-in-rake-tasks/147041/2 "2008-08-27T22:53:15Z")

</div>

On 27 Aug 2008, at 21:18, Cory P.  
\<removed\_email\_address@domain.invalid

> wrote:

> I am writing a rails task that needs to call a controller method.  
> Does  
> anyone know how I can do this?

Not easily. You’re better off pushing the code you need into a model  
or something

Fred

---

<div class="post-metadata">

**Author:** ![corstar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee59a6/32.png) [@corstar](https://www.ruby-forum.com/u/corstar)\
**Post date:** [August 28, 2008, 12:17am UTC](https://www.ruby-forum.com/t/using-controller-methods-in-rake-tasks/147041/3 "2008-08-28T00:17:31Z")

</div>

I am not quite sure how it would work with rake tasks, but when I want a  
plain old ruby script to do work with models (never tried with  
controllers)  
I load the rails environment using (assuming that the folder the script  
resides in the script folder)

require File.dirname( **FILE** ) + ‘/…/config/environment’

On Wed, Aug 27, 2008 at 4:52 PM, Frederick C. \<

---

<div class="post-metadata">

**Author:** ![corstar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee59a6/32.png) [@corstar](https://www.ruby-forum.com/u/corstar)\
**Post date:** [August 28, 2008, 6:31pm UTC](https://www.ruby-forum.com/t/using-controller-methods-in-rake-tasks/147041/4 "2008-08-28T18:31:17Z")

</div>

Ok, I got it working. I moved the method to the model and after some  
looking around, found out how to render from a model.

I used:

av = ActionView::Base.new(Rails::Configuration.new.view\_path)  
product\_description = av.render(:partial =\>  
‘admin/product/prod\_template’…

I passed some locals to the partial, changed the variables in the view  
and everything appears to work.

Of note: you must give the complete relative path of the template in the  
render call or you get the cryptic error:

“undefined method `controller\_path’ for NilClass:Class” instead of the  
normal “no delegate template”

e.g. the template above was located at  
“app/views/admin/product/\_prod\_template.rhtml”

Hopefully this will help someone else.

---

<div class="post-metadata">

**Author:** ![corstar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee59a6/32.png) [@corstar](https://www.ruby-forum.com/u/corstar)\
**Post date:** [August 28, 2008, 6:24am UTC](https://www.ruby-forum.com/t/using-controller-methods-in-rake-tasks/147041/5 "2008-08-28T06:24:32Z")

</div>

I was thinking that too since is really an instance method in my  
application. It worked except for one part, I am using render\_to\_string  
in my controller and I could not figure out how to use that method in my  
model. Is this the job for a helper?

Thanks

Frederick C. wrote:

> On 27 Aug 2008, at 21:18, Cory P.  
> \<removed\_email\_address@domain.invalid
> 
> > wrote:
> 
> > I am writing a rails task that needs to call a controller method.  
> > Does  
> > anyone know how I can do this?
> 
> Not easily. You’re better off pushing the code you need into a model  
> or something
> 
> Fred
