# Recycled objects

**URL:** <https://www.ruby-forum.com/t/recycled-objects/120008>\
**Category:** Rails\
**Created:** [November 12, 2007, 10:36pm UTC](https://www.ruby-forum.com/t/recycled-objects/120008 "2007-11-12T22:36:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rolfa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/779978/32.png) [@rolfa](https://www.ruby-forum.com/u/rolfa)\
**Post date:** [November 12, 2007, 10:36pm UTC](https://www.ruby-forum.com/t/recycled-objects/120008/1 "2007-11-12T22:36:47Z")

</div>

Hi,

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?

Thanks,

Rolf A…

---

<div class="post-metadata">

**Author:** ![rolfa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/779978/32.png) [@rolfa](https://www.ruby-forum.com/u/rolfa)\
**Post date:** [November 12, 2007, 10:40pm UTC](https://www.ruby-forum.com/t/recycled-objects/120008/2 "2007-11-12T22:40:57Z")

</div>

Use :locals when you render your views. like:

\<%= render :partial =\> ‘foo’, :locals =\> {:bar =\> some\_var} %\>

Now in your partial, that variable is available as the local variable  
bar.

-Bill

Rolf A. wrote:

> way? Two, I get an intermittent error, "RangeError : 0xfooba4 is  
> Thanks,
> 
> Rolf A…

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![rolfa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/779978/32.png) [@rolfa](https://www.ruby-forum.com/u/rolfa)\
**Post date:** [November 12, 2007, 10:42pm UTC](https://www.ruby-forum.com/t/recycled-objects/120008/3 "2007-11-12T22:42:07Z")

</div>

On 12 Nov 2007, at 21:36, Rolf A. wrote:

> Hi,
> 
> 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.

Fred

---

<div class="post-metadata">

**Author:** ![rolfa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/779978/32.png) [@rolfa](https://www.ruby-forum.com/u/rolfa)\
**Post date:** [November 12, 2007, 10:47pm UTC](https://www.ruby-forum.com/t/recycled-objects/120008/4 "2007-11-12T22:47:58Z")

</div>

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/](http://www.ruby-forum.com/).
> 
> >

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![rolfa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/779978/32.png) [@rolfa](https://www.ruby-forum.com/u/rolfa)\
**Post date:** [November 13, 2007, 2:28am UTC](https://www.ruby-forum.com/t/recycled-objects/120008/5 "2007-11-13T02:28:59Z")

</div>

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.

Regards

Erik L.

On 13 Nov, 00:27, Rolf A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![rolfa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/779978/32.png) [@rolfa](https://www.ruby-forum.com/u/rolfa)\
**Post date:** [November 13, 2007, 12:27am UTC](https://www.ruby-forum.com/t/recycled-objects/120008/6 "2007-11-13T00:27:51Z")

</div>

> 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.
