# Using Class Variables

**URL:** <https://www.ruby-forum.com/t/using-class-variables/129482>\
**Category:** Rails\
**Created:** [February 18, 2008, 11:34pm UTC](https://www.ruby-forum.com/t/using-class-variables/129482 "2008-02-18T23:34:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [February 18, 2008, 11:34pm UTC](https://www.ruby-forum.com/t/using-class-variables/129482/1 "2008-02-18T23:34:24Z")

</div>

Hello,  
I haven’t used class variables before (I’m new to RoR and OOP in  
general…) and I think I need to use them for part of my program. I  
have several different views for my controller “compare”. For each view,  
I need to grab the number of rows corresponding to several particular  
SQL statements. I was thinking I could do something like this:

class CompareController \< ApplicationController  
@number\_of\_rows = Table.connection.select\_all("  
SELECT  
COUNT(\*) AS n  
FROM table")  
@@numofrows = @number\_of\_rows.to\_s.slice(1,3).to\_i

def index  
etc…

Then use @@numofrows in the view like so:

\<%= link\_to “entries (#{@@numofrows})”, :controller =\> ‘compare’,  
:action =\> ‘index’ %\>

Obviously, this doesn’t work. I am new to this type of programming and I  
am trying to read up on all of this, but I am still very confused on how  
to do this type of thing…

Any help is appreciated!

Thank you,

- Jeff M.

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [February 19, 2008, 3:06am UTC](https://www.ruby-forum.com/t/using-class-variables/129482/2 "2008-02-19T03:06:29Z")

</div>

Nevermind, I dumped the class variable idea and now am using globals  
instead, since they don’t need to be initialized.

Thanks,

- Jeff

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [February 19, 2008, 3:15am UTC](https://www.ruby-forum.com/t/using-class-variables/129482/3 "2008-02-19T03:15:02Z")

</div>

Hi,

I’m not sure I know enough about what you want to do, but I suspect that  
neither class variables nor globals are a good solution. They almost  
never  
are.

If you want the count of rows in some table named “Table,” then you can  
obtain it with Table.count. You could store this in an instance  
variable,  
which will be available in your views. For example, in your controller,

def some\_action  
@row\_count = Table.count  
end

And in your view,

\<%= link\_to “entries (#{@row\_count})”, :controller =\> ‘compare’, :action  
=\>  
‘index’ %\>

If you find yourself fetching the row count in several actions, you  
could  
investigate using a before\_filter.

Good luck,  
Craig

---

<div class="post-metadata">

**Author:** ![sithadmin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@sithadmin](https://www.ruby-forum.com/u/sithadmin)\
**Post date:** [February 19, 2008, 3:46am UTC](https://www.ruby-forum.com/t/using-class-variables/129482/4 "2008-02-19T03:46:25Z")

</div>

Hello,

I advice you to use your model when you run any operation for your  
database.

Here is my code, i hope it can help you.

* * *

## CONTROLLER

class CompareController \< ApplicationController

def index  
…  
end

def count\_row  
@number\_of\_rows = Table.lets\_count\_row  
@slicing = @number\_of\_rows.to\_s.slice(1,3).to\_i  
end

#if you want execute it in any pages of your view in this controller  
#you can do like it

def my\_page  
count\_row  
end

…

end

* * *

## YOUR TABLE MODEL

class Table \< ActiveRecord::Base

def lets\_count\_row  
self.count  
end

OR

def lets\_count\_row  
self.count\_by\_sql(“select count(\*)from tables”)  
end

…

end

* * *

## YOUR VIEW count\_row.rhtml

\<%= link\_to “entries (#{@slicing})”, :controller =\> ‘compare’, :action  
=\> ‘index’ %\>

If you want run def count\_row to entire controller, you should put def  
count\_row in application.rb, and you not need rewrite def count\_row in  
any controller. Example:

* * *

## application.rb

class ApplicationController \< ActionController::Base

def count\_row  
@number\_of\_rows = Table.lets\_count\_row  
@slicing = @number\_of\_rows.to\_s.slice(1,3).to\_i  
end

end

* * *

## any\_name\_controller.rb

class AnyName \< ApplicationController  
before\_filter :count\_row  
…  
end

and the view is the same way and it could be used to any view from any  
controller.

_~_~_~_~_~\*\*~_~_~_~_~_  
Reinhart Ariando  
YM : Booking2Heaven  
WEB : [http://teapoci.blogspot.com](http://teapoci.blogspot.com)
