I am developing a Rails app where I need to pass an object around
between views. I am doing this by sending each view the object_id, and
then doing
@theObject = ObjectSpace._id2ref(params[:objid])
There are two problems with this: One, it’s not very elegant. Is there a
better
way? Two, I get an intermittent error, “RangeError : 0xfooba4 is
recycled object”. I suspect this happens because my object is getting
garbage collected
between views. Since it is an instance member of my controller object
(I’ve also
experimented with making it a class member), I feel this ought not to
happen,
but there it is. Can anyone suggest a better method for persisting my
object
so all my views have access to it, and it doesn’t get garbage collected?
I am developing a Rails app where I need to pass an object around
between views. I am doing this by sending each view the object_id, and
then doing
@theObject = ObjectSpace._id2ref(params[:objid])
This is a really bad idea (and of course were you ever to have more
than one mongrel then the 2 requests could be server by completely
separate processes, which means that it would be completely impossible
for this to ever work).
happen,
but there it is.
A given instance of controller only lasts for one request. I suspect
you’re running in dev mode, so classes are also reloaded after each
request.
Can anyone suggest a better method for persisting my
object
so all my views have access to it, and it doesn’t get garbage
collected?
Serialize it and store the data in the db, memcached or even the
session.
Hmm, ok, I took him to mean views as, well, views. If you indeed mean
between page requests, then yes, store them in the session or another
persistent area. However, if you really do mean between separate views
in during the same request, then :locals is what you want.
-Bill
Frederick C. wrote:
better
A given instance of controller only lasts for one request. I suspect
Serialize it and store the data in the db, memcached or even the
Posted via http://www.ruby-forum.com/.
Most web apps do database reads on every request. If it is semi-static
you can always use page or view caching if it turns out to be a
bottleneck. For a web server the load must be very high (wrt request/
second) before the db becomes the bottleneck, especially for a Rails
app.
Regarding sessions they (to me) seem to be what you want to use. There
is not a problem with different users. The point of a session is that
the web server creates a specific session for each user. So if you
write session[:user_name] = params[:name] in a controller it will
store different values for different web users. If the objects are
large they should not be stored in the sessions, there are multiple
articles around on this but I don’t have any references handy.
If you need further tips, please specify more precisely what you want
to store between requests.
A given instance of controller only lasts for one request. I suspect
you’re running in dev mode, so classes are also reloaded after each
request.
I see. That explains it, yes.
Serialize it and store the data in the db, memcached or even the
session.
You really advise me to read from the database at every request? Seems a
little inefficient. I can’t use the session, since the object (a game
state) includes some information that shouldn’t be available to all
users. Can you expand on memcaching?
Thanks for your replies so far.
Rolf A.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.