setting a "global" variable..

Hi --

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..

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? :slight_smile:

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

Hi --

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. :slight_smile:
It's one of the great things about Ruby and RoR that I hope we will
never lose.