# Rails and Singeltons

**URL:** <https://www.ruby-forum.com/t/rails-and-singeltons/159051>\
**Category:** Rails\
**Created:** [January 30, 2009, 8:43pm UTC](https://www.ruby-forum.com/t/rails-and-singeltons/159051 "2009-01-30T20:43:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![balla123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@balla123](https://www.ruby-forum.com/u/balla123)\
**Post date:** [January 30, 2009, 8:43pm UTC](https://www.ruby-forum.com/t/rails-and-singeltons/159051/1 "2009-01-30T20:43:12Z")

</div>

Does it make sense to make a singleton class in Rails? I made a  
singelton class but everytime I do model.instance.id in some controller  
it is always different. Is this because it is in development mode so the  
classes get reloaded each time but in production this would not be the  
case?

---

<div class="post-metadata">

**Author:** ![balla123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@balla123](https://www.ruby-forum.com/u/balla123)\
**Post date:** [January 30, 2009, 9:10pm UTC](https://www.ruby-forum.com/t/rails-and-singeltons/159051/2 "2009-01-30T21:10:38Z")

</div>

The short and sweet: you can not, should not, and will not try to save  
information in memory in your Rails environment across web requests.  
If you need to save data, use a database, use the session, use flat  
files on the disk. Expecting Ruby to “just remember” is pointless here  
because 1) yes, development environment reloads everything on each  
request and 2) production deploy environments run mutiple instances of  
Rails processes so you’re never guarenteed to get the same process for  
subsequent requests.

Jason

On Fri, Jan 30, 2009 at 2:43 PM, Ball B.

---

<div class="post-metadata">

**Author:** ![balla123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@balla123](https://www.ruby-forum.com/u/balla123)\
**Post date:** [January 30, 2009, 9:20pm UTC](https://www.ruby-forum.com/t/rails-and-singeltons/159051/3 "2009-01-30T21:20:40Z")

</div>

Thanks jason. Question on part 2)

Say I have only 1 production box with 4 mongrels on it. Wouldnt there  
only be 4 rails processes? Or does each mongrel have many rails  
processes running? Do you know of any documentation that explains all of  
this? Thanks

Jason R. wrote:

> The short and sweet: you can not, should not, and will not try to save  
> information in memory in your Rails environment across web requests.  
> If you need to save data, use a database, use the session, use flat  
> files on the disk. Expecting Ruby to “just remember” is pointless here  
> because 1) yes, development environment reloads everything on each  
> request and 2) production deploy environments run mutiple instances of  
> Rails processes so you’re never guarenteed to get the same process for  
> subsequent requests.
> 
> Jason
> 
> On Fri, Jan 30, 2009 at 2:43 PM, Ball B.

---

<div class="post-metadata">

**Author:** ![balla123](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@balla123](https://www.ruby-forum.com/u/balla123)\
**Post date:** [January 30, 2009, 10:39pm UTC](https://www.ruby-forum.com/t/rails-and-singeltons/159051/4 "2009-01-30T22:39:49Z")

</div>

Rails deployment is a huge subject, though in short if you were to use  
Mongrel you’d have your HTTP server in front (Apache / nginx / etc)  
proxy out to a cluster of mongrels. Thus, each mongrel is a Rails  
process.

Personally, I’d say to forego Mongrels / Thins / etc and check out  
Passenger: [http://www.modrails.com](http://www.modrails.com) (which does the same thing,  
proxying off to Rails processes, but it’s all hidden from you).

Jason

On Fri, Jan 30, 2009 at 3:20 PM, Ball B.
