views:

392

answers:

2

I've got a cron job parsing a few XML feeds every ten minutes and creating a few partials to include on every page in my Rails project with the render: file method. The problem is that, obviously, they don't refresh until the server restarts.

Anyway, what can I do to force rails to refresh this file every time?

Thanks in advance.

+1  A: 

This happens because in production mode Rails cashes all its classes and views/partials. To solve this problem you can render your partials inside fragment caching block and use method expire_fragment when you want to refresh block content. Other way can be switching to the development mode but it will make your application slower.

IDBD
Solution is CERTAINLY not running the app in Development. That's a terrible idea. A less terrible, but still awful idea is to turn on class reloading in production, but this is pretty much worst case scenario.
BJ Clark
+2  A: 

I think you are misusing render(:file). This is definitely not how it is intended to be used. Rendering a partial is supposed to be used to render a modular piece of erb (or whatever template lang you use) code in multiple places, not a data-load technique. You're using it (it appears) to load dynamically changing data.

I think what you're really trying to do is fragment caching, but without more context, I could be wrong.

For a great walkthrough on fragment caching: http://www.railsenvy.com/2007/3/20/ruby-on-rails-caching-tutorial-part-2

Probably your cron job would run a script/runner script that would create the fragment you're looking to cache.

If I'm off base here, please try to clarify what type of file you're loading and what you're trying to accomplish.

BJ Clark
I agree. Sounds like a fragment cache would do the job. Have the cron job expire the fragment cache and then the erb will run through and they'll be cached next time they are displayed.
Shadwell