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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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? > > Thanks, > Michael > > > -- > Posted via http://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
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 Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Clayton Cottingham
2006-Sep-04 19:18 UTC
Re: Best way to implement application-wide variables?
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 %> -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Josh Susser Sent: Monday, September 04, 2006 11:32 AM To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails] Re: Best way to implement application-wide variables? 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 Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Josh Susser 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 Susser > http://blog.hasmanythrough.comThe 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---