views:

442

answers:

2

I'm creating a tableless Rails model, and am a bit stuck on how I should use it.

Basically I'm trying to create a little application using Feedzirra that scans a RSS feed every X seconds, and then sends me an email with only the updates.

I'm actually trying to use it as an activerecord model, and although I can get it to work, it doesn't seem to "hold" data as expected.

As an example, I have an initializer method that parses the feed for the first time. On the next requests, I would like to simply call the get_updates method, which according to feedzirra, is the existing object (created during the initialize) that gets updated with only the differences.

I'm finding it really hard to understand how this all works, as the object created on the initialize method doesn't seem to persist across all the methods on the model.

My code looks something like:

def initialize
     feed parse here
end

def get_updates
     feedzirra update passing the feed object here
end

Not sure if this is the right way of doing it, but it all seems a bit confusing and not very clear. I could be over or under-doing here, but I'd like your opinion about this approach.

Thanks in advance

A: 

The instance variable will not persist between requests since they are entirely different instances. You will likely want to store the feed data in a database so it can be saved between requests and updated after the next request.

Beerlington
But that's exactly what i didn't wanna do. Isn't there a way to save it on a "server scope" for example? So as long as the server is up, the feed will be available?
Marcos Placona
I'm not sure why I didn't think of this before but memcached is another option. It's basically a key/value memory store that doesn't require a database. Rails has built in support for it but you'll need more control over your hosting environment. I haven't personally used it within rails, but just in some Ruby scripts and it works really well.http://www.ridingtheclutch.com/2009/01/08/cache-anything-easily-with-rails-and-memcached.html
Beerlington
+1  A: 

Using the singleton design pattern it is possible to keep values in memory between requests in ruby on rails. Rails does not reload all objects on every request, so it is possible to keep an in memory store.

with the following in config/initializers/xxx

require 'singleton'
class PersistanceVariableStore
  include Singleton
  def set(val)
    @myvar = val
  end
  def get
    @myvar
  end
end

In a controller for example :

@r = PersistanceVariableStore.instance
@r.set(params[:set]) if params[:set]

Then in a view :

<%= @r.get %>

The value in @r will persist between requests ( unless running in cgi mode ).

Not that I think this is a good idea...

Eadz