Hi all,
I thought I had found a way to have my info about the logged in user
handy everywhere by putting this in application.rb:
===============================
Provide an application-wide variable @current_org_id
attr_reader :current_org_id
before_filter :get_current_org_id
def get_current_org_id
@current_org_id = session[‘org_id’]
end
===============================
…but when I try to use this instance variable from inside a Company
object being saved…
===============================
def before_create
# Link new company to the organization logged in.
self.organization_id = @current_org_id
end
================================
…the instance variable is Nil. It works fine in controllers and views,
but not inside this model method. Wassup?
I also tried using the session[‘org_id’] value directly but that yields
a missing method error.
In sum…what’s the best way to create and access application-wide
variables without using a [slow] data record?
Thanks,
Michael
You really ought to consider rethinking why you need a global variable
like this. Models are meant to stand along and be manipulated through
an independent interface. So although you modify them from a
controller, you can also use them in the console, in unit tests, or
even completely separate ruby programs. Requiring a global variable
instantly places a very specific constraint on all these possible uses.
That said, I see what you’re trying to do, but I’m not sure of the best
solution exactly. There must be a clean way to do it, because it’s a
cornerstone of 37signals apps. Maybe someone else can explain the best
way to do it.
Michael Wilkes wrote:
Hi all,
I thought I had found a way to have my info about the logged in user
handy everywhere by putting this in application.rb:
===============================
Provide an application-wide variable @current_org_id
attr_reader :current_org_id
before_filter :get_current_org_id
def get_current_org_id
@current_org_id = session[‘org_id’]
end
===============================
…but when I try to use this instance variable from inside a Company
object being saved…
===============================
def before_create
# Link new company to the organization logged in.
self.organization_id = @current_org_id
end
================================
…the instance variable is Nil. It works fine in controllers and views,
but not inside this model method. Wassup?
I also tried using the session[‘org_id’] value directly but that yields
a missing method error.
In sum…what’s the best way to create and access application-wide
variables without using a [slow] data record?
The session hash is not available to model objects, only to controllers
and views. Models should be functional in the console, xml feeds,
mailers, etc, so you can’t have them rely on session state for browser
users. Going the global variable route is also a bad idea. Your model is
still dependent on the session, but the dependency is now indirect. Ugh.
It’s best to design your models so that there is one that represents the
organization, then work with that from your controller action. Without
knowing more about your code there isn’t much more I can offer.
–
Josh S.
http://blog.hasmanythrough.com
I don’t know if this is proper or not
But lately ive been doing something like this in application.rb
$globals=Globals.find(1)
Then anywhere you need the global data you just
<%= $global.bgcolor %>
Josh S. wrote:
The session hash is not available to model objects, only to controllers
and views. Models should be functional in the console, xml feeds,
mailers, etc, so you can’t have them rely on session state for browser
users. Going the global variable route is also a bad idea. Your model is
still dependent on the session, but the dependency is now indirect. Ugh.
It’s best to design your models so that there is one that represents the
organization, then work with that from your controller action. Without
knowing more about your code there isn’t much more I can offer.
–
Josh S.
http://blog.hasmanythrough.com
The best thing is probably to just pass in data you want to model
methods that need it. This keeps the model accessible via console and
all that other jazz.