# Class Variable

**URL:** <https://discuss.rubyonrails.org/t/class-variable/60617>\
**Category:** rubyonrails-talk\
**Created:** [August 28, 2011, 11:16am UTC](https://discuss.rubyonrails.org/t/class-variable/60617 "2011-08-28T11:16:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [August 28, 2011, 11:16am UTC](https://discuss.rubyonrails.org/t/class-variable/60617/1 "2011-08-28T11:16:50Z")

</div>

Hi everyone,

I have a question about class variable in Rails:

I have a class Point that contains an class variable @@color\_points="green"

In a first web Page I want to set up this variable, hence i do Point.color\_points = "green" then i test it using puts Point.color\_points ( = green, it works,yes!) Then i have a link\_to instruction that points to another web page. On this one (the other one), I just want to print Point.color\_points, but now its value is "".

DOes anyone know why and could explain me?

---

<div class="post-metadata">

**Author:** ![Michael\_Pavling](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Michael\_Pavling](https://discuss.rubyonrails.org/u/Michael_Pavling)\
**Post date:** [August 28, 2011, 11:19am UTC](https://discuss.rubyonrails.org/t/class-variable/60617/2 "2011-08-28T11:19:52Z")

</div>

Each request is isolated. At the end of the first request, all the variables you've set disappear into the ether. If you want something to persist, you need to store it - in the DB, the session, or somewhere else. The second request builds up new instances of all the objects - so the class variable is back to its default value.

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [August 28, 2011, 1:55pm UTC](https://discuss.rubyonrails.org/t/class-variable/60617/3 "2011-08-28T13:55:04Z")

</div>

thelo.g thelo wrote in post #1018888:

> Hi everyone,
> 
> I have a question about class variable in Rails:
> 
> I have a class Point that contains an class variable @@color\_points="green"
> 
> In a first web Page I want to set up this variable, hence i do Point.color\_points = "green" then i test it using puts Point.color\_points ( = green, it works,yes!) Then i have a link\_to instruction that points to another web page. On this one (the other one), I just want to print Point.color\_points, but now its value is "".
> 
> DOes anyone know why and could explain me?

In addition, you shouldn't be using class variables. You can forget they exist.

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [August 28, 2011, 2:14pm UTC](https://discuss.rubyonrails.org/t/class-variable/60617/4 "2011-08-28T14:14:00Z")

</div>

class Dog &nbsp;&nbsp;@count = 0

&nbsp;&nbsp;def initialize &nbsp;&nbsp;&nbsp;&nbsp;Dog.count += 1 &nbsp;&nbsp;end

&nbsp;&nbsp;def self.count &nbsp;&nbsp;&nbsp;&nbsp;@count &nbsp;&nbsp;end

&nbsp;&nbsp;def self.count=(val) &nbsp;&nbsp;&nbsp;&nbsp;@count = val &nbsp;&nbsp;end

end

d1 = Dog.new puts Dog.count

d2 = Dog.new puts Dog.count

--output:-- 1 2

---

<div class="post-metadata">

**Author:** ![Michael\_Pavling](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Michael\_Pavling](https://discuss.rubyonrails.org/u/Michael_Pavling)\
**Post date:** [August 28, 2011, 2:23pm UTC](https://discuss.rubyonrails.org/t/class-variable/60617/5 "2011-08-28T14:23:36Z")

</div>

Bwah! Hahahah!

What tosh...

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [August 28, 2011, 2:56pm UTC](https://discuss.rubyonrails.org/t/class-variable/60617/6 "2011-08-28T14:56:55Z")

</div>

> class Dog @count = 0
> 
> def initialize Dog.count += 1 end
> 
> def self.count @count end
> 
> def self.count=(val) @count = val end
> 
> end

If you want to be entirely nit-picky those are class instance variables rather than class variables.

Fred

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [August 28, 2011, 11:34pm UTC](https://discuss.rubyonrails.org/t/class-variable/60617/7 "2011-08-28T23:34:18Z")

</div>

Michael Pavling wrote in post #1018902:
