How can I call a controller action from a rake task?
I have a controller action that generates a number of excel reports, this takes about 10 minutes to do. Sometimes I call it from my webapp, which is why it is an action.
But I also want to create a开发者_运维问答 rake task to run this, so I can schedule it to automatically run once a night.
Any way to do this?
Can you handle the report generation from your models? Models should be doing most of the work anyway and can be accessed from Rake tasks:
task :reports => :environment do
...
# Do stuff with your models.
end
I think you'll have to move your code into your model. Since it's bad to put knowledge about output rendering in models, I'd suggest putting all the business logic and data manipulation in the model, but then put the rendering code in your rake task. That would make the rake task analogous to the controller used on the web - maintaining separation of concerns.
You can look at ActionView::Base and work from there to figure out how to trigger rendering of templates.
精彩评论