# Setting a "global" variable

**URL:** <https://www.ruby-forum.com/t/setting-a-global-variable/79547>\
**Category:** Rails\
**Created:** [December 16, 2006, 8:26am UTC](https://www.ruby-forum.com/t/setting-a-global-variable/79547 "2006-12-16T08:26:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![spacechurro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@spacechurro](https://www.ruby-forum.com/u/spacechurro)\
**Post date:** [December 16, 2006, 8:26am UTC](https://www.ruby-forum.com/t/setting-a-global-variable/79547/1 "2006-12-16T08:26:55Z")

</div>

hi, all…

i have an activerecord object called:

@site\_info = SiteInfo.find(:first)

that is used on pretty much every page on my site…

i am finding that i have to set that all over the place in my app…

is it possible to put this declaration somewhere where i can find do:

@site\_info.site\_name

anywhere in my app?

i have tried putting it in application.rb, and at the top of all my  
controllers, with no luck…

any help would be appreciated…

thanks!

---

<div class="post-metadata">

**Author:** ![spacechurro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@spacechurro](https://www.ruby-forum.com/u/spacechurro)\
**Post date:** [December 16, 2006, 12:25pm UTC](https://www.ruby-forum.com/t/setting-a-global-variable/79547/2 "2006-12-16T12:25:33Z")

</div>

Why not using global variable?  
$site\_info = SiteInfo.find(:first)

and all of your rails files can access $site\_info.

---

<div class="post-metadata">

**Author:** ![spacechurro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@spacechurro](https://www.ruby-forum.com/u/spacechurro)\
**Post date:** [December 16, 2006, 3:41pm UTC](https://www.ruby-forum.com/t/setting-a-global-variable/79547/3 "2006-12-16T15:41:40Z")

</div>

Hi –

On Sat, 16 Dec 2006, Sergio R. wrote:

> is it possible to put this declaration somewhere where i can find do:
> 
> @site\_info.site\_name
> 
> anywhere in my app?
> 
> i have tried putting it in application.rb, and at the top of all my  
> controllers, with no luck…

I suspect you’ve done something like this:

class ApplicationController \< ActionController::Base

```
 @site_info = SiteInfo.find(:first)

```

and then in another controller:

```
 def do_something
   name = @site_info.site_name
 end
 ...

```

end

and gotten a NoMethodError for calling “site\_name” on nil.

Am I close? 🙂

If that’s the case, then the problem is that you’ve defined the  
instance variable to be a property of the class object  
ApplicationController – which, because of how instance variables work  
in Ruby (each one belongs to exactly one object) means that it’s not  
available where you really need it to be: namely, the controller  
_instance_.

So, try this:

class ApplicationController \< ACB  
before\_filter set\_site\_info  
def set\_site\_info  
@site\_info = SiteInfo.find(:first)  
end

```
 ...

```

end

That way, every controller instance will set the variable, and it will  
be visible to the controller (and the view, thanks to some “magic”  
behind the scenes) for the duration of the request.

David

–  
Q. What’s a good holiday present for the serious Rails developer?  
A. RUBY FOR RAILS by David A. Black ([Ruby for Rails](http://www.manning.com/black))  
aka The Ruby book for Rails developers!  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![spacechurro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@spacechurro](https://www.ruby-forum.com/u/spacechurro)\
**Post date:** [December 19, 2006, 4:30am UTC](https://www.ruby-forum.com/t/setting-a-global-variable/79547/4 "2006-12-19T04:30:57Z")

</div>

Hi –

On Tue, 19 Dec 2006, Sergio R. wrote:

> > ```
> > ...
> > 
> > ```
> 
> NameError (undefined local variable or method `set\_site\_info’ for  
> ApplicationController:Class):

Whoops, I left the : off of :set\_site\_info. Sorry. It should be:

before\_filter :set\_site\_info

David

–  
Q. What’s a good holiday present for the serious Rails developer?  
A. RUBY FOR RAILS by David A. Black ([Ruby for Rails](http://www.manning.com/black))  
aka The Ruby book for Rails developers!  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![spacechurro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@spacechurro](https://www.ruby-forum.com/u/spacechurro)\
**Post date:** [December 19, 2006, 3:40am UTC](https://www.ruby-forum.com/t/setting-a-global-variable/79547/5 "2006-12-19T03:40:30Z")

</div>

> So, try this:
> 
> class ApplicationController \< ACB  
> before\_filter set\_site\_info  
> def set\_site\_info  
> @site\_info = SiteInfo.find(:first)  
> end
> 
> ```
> ...
> 
> ```
> 
> end

hi!

thanks so much for your help…

i tried doing in this way, but i am having no luck…

i am getting this error now:

NameError (undefined local variable or method `set\_site\_info’ for  
ApplicationController:Class):

i am thinking about adding:

\<%- @site\_info = SiteInfo.find(:first) %\>

into my layout… but i really don’t like doing business in the  
template…

any ideas why i might be having a the above error?

thanks!

---

<div class="post-metadata">

**Author:** ![spacechurro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@spacechurro](https://www.ruby-forum.com/u/spacechurro)\
**Post date:** [December 19, 2006, 4:58am UTC](https://www.ruby-forum.com/t/setting-a-global-variable/79547/6 "2006-12-19T04:58:52Z")

</div>

> Whoops, I left the : off of :set\_site\_info. Sorry. It should be:
> 
> before\_filter :set\_site\_info

hi, david!

thanks so much for your help!

btw…

i just ordered your book… based on your sig… so, if you are wondering  
if it works, it does…

thanks again!

---

<div class="post-metadata">

**Author:** ![spacechurro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@spacechurro](https://www.ruby-forum.com/u/spacechurro)\
**Post date:** [December 19, 2006, 5:18pm UTC](https://www.ruby-forum.com/t/setting-a-global-variable/79547/7 "2006-12-19T17:18:12Z")

</div>

Funny - it went the other way for me: I purchased the book the learn  
about ruby and rails, then find the author is quite active here. 🙂  
It’s one of the great things about Ruby and RoR that I hope we will  
never lose.

On Dec 19, 2:58 pm, Sergio R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
