# Where to call a global variable?

**URL:** <https://www.ruby-forum.com/t/where-to-call-a-global-variable/153350>\
**Category:** Rails\
**Created:** [November 15, 2008, 11:16am UTC](https://www.ruby-forum.com/t/where-to-call-a-global-variable/153350 "2008-11-15T11:16:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![panatura](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@panatura](https://www.ruby-forum.com/u/panatura)\
**Post date:** [November 15, 2008, 11:16am UTC](https://www.ruby-forum.com/t/where-to-call-a-global-variable/153350/1 "2008-11-15T11:16:19Z")

</div>

Hi guys,

I my application has to be all the “recent news, recent users, recent  
pictures”-stuff. This is shown in a rendered view. Right now I’m using a  
site controller getting data from different models like

@news = Article.find(:all, :limit =\> 3, :order =\> ‘created\_at DESC’)

Of course that doesn’t work anymore calling different contollers. I  
thought about setting a global variable? I tried to put it in  
apllication\_helper but I get a nil-error by writing

def recent  
$news = Article.find(:all, :limit =\> 3, :order =\> ‘created\_at DESC’)  
end

and calling the following in the render

\<% for news in $news %\>

##### \<%=h news.title %\>

\<%=h truncate(news.synopsis, 200) %\>

mehr...

\<% end %\>

Is it fine to work with a global variable and if how to do that?

Greetings  
Mayo

---

<div class="post-metadata">

**Author:** ![panatura](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@panatura](https://www.ruby-forum.com/u/panatura)\
**Post date:** [November 15, 2008, 11:21am UTC](https://www.ruby-forum.com/t/where-to-call-a-global-variable/153350/2 "2008-11-15T11:21:47Z")

</div>

…well,

of course I have to call \<%= recent %\>, but anyway it’s empty.

---

<div class="post-metadata">

**Author:** ![panatura](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@panatura](https://www.ruby-forum.com/u/panatura)\
**Post date:** [November 15, 2008, 1:00pm UTC](https://www.ruby-forum.com/t/where-to-call-a-global-variable/153350/3 "2008-11-15T13:00:52Z")

</div>

…solved, “I did it my way”

Site-Controller

def index  
@title =“Willkommen”  
@news = Article.find(:all, :limit =\> 3, :order =\> ‘created\_at DESC’)  
$news = @news  
end

Right now I don’t know what happens with the variable at all. Looks like  
that way, any controller, even called straight from a different site  
will participate from the variable called and set by any user.

Every time a user calls the site-controller (well and it’s the  
index-site) the variable will be updated.

---

<div class="post-metadata">

**Author:** ![panatura](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@panatura](https://www.ruby-forum.com/u/panatura)\
**Post date:** [November 15, 2008, 5:30pm UTC](https://www.ruby-forum.com/t/where-to-call-a-global-variable/153350/4 "2008-11-15T17:30:32Z")

</div>

This doesn’t seem like the best way to use a global variable. If you are  
doing something like displaying those 3 articles on lots of pages, then  
maybe you want something like this in application.rb:  
before\_filter :get\_recent\_articles

def get\_recent\_articles  
@articles = Article.recent  
end

Then you will be able to access @articles in every view (with \<%  
articles.each do |article| %\> etc). And you will have access to  
@articles in all your controller actions.

You won’t have access to @articles in your models though. But there you  
could just call Article.recent.

Note: the above code uses a named scope like this (in article.rb)  
named\_scope :recent, :limit =\> 3, :order =\> ‘created\_at DESC’

What I’m suggesting doesn’t sound perfect to me (I’m just writing this  
quickly). But I think it is a better way to organize it than I global  
variable.

---

<div class="post-metadata">

**Author:** ![panatura](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@panatura](https://www.ruby-forum.com/u/panatura)\
**Post date:** [November 15, 2008, 8:47pm UTC](https://www.ruby-forum.com/t/where-to-call-a-global-variable/153350/5 "2008-11-15T20:47:03Z")

</div>

… thanks Pau,

indeed you’re right, and by the way, the same way has been suggested in  
a different board.

Thanks alot, greetings  
Mayo

---

<div class="post-metadata">

**Author:** ![panatura](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@panatura](https://www.ruby-forum.com/u/panatura)\
**Post date:** [November 16, 2008, 3:28pm UTC](https://www.ruby-forum.com/t/where-to-call-a-global-variable/153350/6 "2008-11-16T15:28:40Z")

</div>

I have always like constants for “global” variables. It comes from  
programming C I think. Having model related constants in your  
controllers is really not the MVC way IMHO. A constant is easy to  
setup in ActiveRecord and in my opinion are much more readable. And  
guess what, I actually did it with an Article model too! =\>

class Article \< ActiveRecord::Base  
…  
ARTS\_CATEGORY = Category.find(:first, :conditions =\> [‘name LIKE ?’,  
‘arts%’]).id  
OPINE\_CATEGORY = Category.find(:first, :conditions =\> [‘name  
LIKE ?’, ‘opine%’]).id  
DISTRACTIONS\_CATEGORY = Category.find(:first, :conditions =\> [‘name  
LIKE ?’, ‘distra%’]).id  
NEWS\_POLITICS\_CATEGORY = Category.find(:first, :conditions =\> [‘name  
LIKE ?’, ‘news%’]).id

…

So in my controllers I use a method like this (the actual method is a  
shared method for all “types” longer, this is for readability:  
…  
def arts\_archive  
Article.find\_by\_category\_id(Article::ARTS\_CATEGORY)  
end  
…

On Nov 15, 2:47 pm, Mario Peterscheck \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![panatura](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@panatura](https://www.ruby-forum.com/u/panatura)\
**Post date:** [November 16, 2008, 4:25pm UTC](https://www.ruby-forum.com/t/where-to-call-a-global-variable/153350/7 "2008-11-16T16:25:40Z")

</div>

blasterpal wrote:

> class Article \< ActiveRecord::Base  
> …  
> ARTS\_CATEGORY = Category.find(:first, :conditions =\> [‘name LIKE ?’,  
> ‘arts%’]).id  
> OPINE\_CATEGORY = Category.find(:first, :conditions =\> [‘name  
> LIKE ?’, ‘opine%’]).id  
> DISTRACTIONS\_CATEGORY = Category.find(:first, :conditions =\> [‘name  
> LIKE ?’, ‘distra%’]).id  
> NEWS\_POLITICS\_CATEGORY = Category.find(:first, :conditions =\> [‘name  
> LIKE ?’, ‘news%’]).id
> 
> …
> 
> So in my controllers I use a method like this (the actual method is a  
> shared method for all “types” longer, this is for readability:  
> …  
> def arts\_archive  
> Article.find\_by\_category\_id(Article::ARTS\_CATEGORY)  
> end  
> …

I think a better way to do this is

class Category \< ActiveRecord::Base  
has\_many :articles #or has\_one :article  
def self.arts  
# You could probably also do this with a named scope,  
# but I don’t remember the syntax off the top of my  
# for selecting just one object.  
Category.find(:first, :conditions =\> [‘name LIKE ?’, ‘arts%’]).id  
end  
end

and then in your controller:  
Category.arts.articles
