Topic: app.get in controller or model?

Hello,

in the rails console you can use/call app.get '/index'

is ist possible to do the same in a controller or model?

I need this to render a page after a expire_page.

Regards
Guido

Re: app.get in controller or model?

Use redirect_to some_path where some_path is the value of a route that points to the cotroller and action you want to call
You should read through this http://guides.rubyonrails.org/getting_started.html

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: app.get in controller or model?

Hi jamesw,

Thx for your answer!

I see I have to tell more abaut the background of the problem.

I use page caching and i want that after a expire_page the page immediately is rendered and stored in the cache.

I want to avoid that the user who hit the page first has to wait till the page is rendered ...

I want to shoot directly form the cache ;-)

Re: app.get in controller or model?

ok, I auume you are asking how to re cach a page after it expires?
I think that would be bad practice. There are many reasons why a cache could be expired and this could be triggered a number of times in a perceived cycle of events causing additional unnecessary load on your server.
There is only one reason why you would want to do this, which is that it takes a long time to gather the data to load the page which implies you should be concentrating on speeding up the data retrieval rather than trying to re-cache.

Perhaps a better, more scaleable approah would be to create a table view to represent the data you are needing or fake a table view by using json. If after the cache is swept you store the data in a json file you could then load the page from the json rather than directly from the database you would find a massive improvement in performance without needing to re-cache.

Those are better alternatives but neither answers your question. If you really really want to do this then the best approach would be to

Create a rake task that would use curl to visit whatever paths you need to re-cache then call that rake task from your sweeper.
A rake task has the benefit of being able to be called from wiothing a model or controller.

ALWAYS work harder to speed up the data retrieval in the first place! In addition to the table views and json approach mentioned above you should take a look at your table indexing and improving query performance.

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: app.get in controller or model?

yes I want to re cach a page after it expires.

And it is the one reason ;-) 25 SQL Qs to 3 different DB ... time to load 2-5 minutes

The changes are not that often. To have a long time to load causes more pain than the server load for the re cach ...

Till now I never made a rake task or a background job ... I see I have to learn it.

And i will call the page with the build in ActiveRessource.

I am still a noob and i am wondering why I only can call this app.get in the rails console.

It is somehow in the ActionDispatch::Integration::RequestHelpers but i don´t know hove to include and call it.

Re: app.get in controller or model?

i am wondering why I only can call this app.get in the rails console

app.get  some_path 

is just a way of hitting a route from your console. It's equivalent in a controller is

redirect_to some_path

The issue you are facing is that you are trying to render a page outside of a controller (in your cache sweeper) and that is just plain wrong. It's a controllers job to render pages.

Till now I never made a rake task or a background job

Rake tasks are dead easy.
http://railscasts.com/episodes/66-custom-rake-tasks
It's an old cast - There might be a newer one - so be careful of the syntax.

Background tasks - There are many options for running background tasks in rails most of which are complete overkill for this specific case. The simplest option is to just fork the rake task into a new process when you call it.

http://railscasts.com/episodes/127-rake-in-background.
Again an old cast so watch the syntax

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)