Lucas Campbell
2008-Feb-25 06:11 UTC
Using global variable to access model from controller
Don''t know if this is possible but I am trying to save myself a lot of repeat code. I have a global variable $school in my application helper which is used to keep track of which school i need to access. In my controller I have several different databases i need to access depending on which school I am using. To do this I have the following code in my controller: $school.find(:all) where $school == my model that I want to query. $school doesnt seem to resolve correctly so that my database is queried. In my application helper i set $school = "Item" or any other string. Is there a way to do this so I don''t have to write several if statements to determine the model i want to access? -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Feb-25 10:45 UTC
Re: Using global variable to access model from controller
On 25 Feb 2008, at 06:11, Lucas Campbell wrote:> > Don''t know if this is possible but I am trying to save myself a lot of > repeat code. > > I have a global variable $school in my application helper which is > used > to keep track of which school i need to access. > > In my controller I have several different databases i need to access > depending on which school I am using. To do this I have the following > code in my controller: > > $school.find(:all) where $school == my model that I want to query. > > $school doesnt seem to resolve correctly so that my database is > queried. > > In my application helper i set $school = "Item" or any other string. > > Is there a way to do this so I don''t have to write several if > statements > to determine the model i want to access?This doesn''t sound like a good idea at all. If you do want to be able to do $school.find ... then $school needs to be a class, not a string containing the name of the class, ie set $school = MyActiveRecordClass Fred --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
Lucas Campbell
2008-Feb-25 18:53 UTC
Re: Using global variable to access model from controller
Frederick Cheung wrote:> On 25 Feb 2008, at 06:11, Lucas Campbell wrote: > >> code in my controller: >> to determine the model i want to access? > This doesn''t sound like a good idea at all. > If you do want to be able to do $school.find ... then $school needs to > be a class, not a string containing the name of the class, ie set > $school = MyActiveRecordClass > FredWhat would the command look like? I have a class class Item < ActiveRecord::Base i tried setting: $school = Item < ActiveRecord::Base but that doesn''t work. -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
Lucas Campbell
2008-Feb-25 19:12 UTC
Re: Using global variable to access model from controller
Lucas Campbell wrote:> Frederick Cheung wrote: >> On 25 Feb 2008, at 06:11, Lucas Campbell wrote: >> >>> code in my controller: >>> to determine the model i want to access? >> This doesn''t sound like a good idea at all. >> If you do want to be able to do $school.find ... then $school needs to >> be a class, not a string containing the name of the class, ie set >> $school = MyActiveRecordClass >> Fred > > What would the command look like? I have a class class Item < > ActiveRecord::Base > > i tried setting: > > $school = Item < ActiveRecord::Base > > but that doesn''t work.I found an answer. I used: (Object.const_get($school)).find(:all) -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
Stephan Wehner
2008-Feb-25 20:14 UTC
Re: Using global variable to access model from controller
Lucas Campbell wrote:> Lucas Campbell wrote: >> Frederick Cheung wrote: >>> On 25 Feb 2008, at 06:11, Lucas Campbell wrote: >>> >>>> code in my controller: >>>> to determine the model i want to access? >>> This doesn''t sound like a good idea at all. >>> If you do want to be able to do $school.find ... then $school needs to >>> be a class, not a string containing the name of the class, ie set >>> $school = MyActiveRecordClass >>> Fred >> >> What would the command look like? I have a class class Item < >> ActiveRecord::Base >> >> i tried setting: >> >> $school = Item < ActiveRecord::Base >> >> but that doesn''t work. > > I found an answer. I used: > > (Object.const_get($school)).find(:all)In which case, replacing $school = .... your expression ... with $school = Object.const_get(... your expression ...) looks a little more useful. For more transparency you could also try eval "class School < #{YOUR_DESIRED_CLASS}; end" in other words create the class on the fly. Then in the controller code, etc, you just do the usual School.find.... Stephan -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Feb-26 08:45 UTC
Re: Using global variable to access model from controller
On 25 Feb 2008, at 20:14, Stephan Wehner wrote:>>> i tried setting: >>> >>> $school = Item < ActiveRecord::Base >>> >>> but that doesn''t work. >> >> I found an answer. I used: >> >> (Object.const_get($school)).find(:all) >$school = Item would have worked> In which case, replacing > > $school = .... your expression ... > > with > > $school = Object.const_get(... your expression ...) > > looks a little more useful. > > > > > > For more transparency you could also try > > eval "class School < #{YOUR_DESIRED_CLASS}; end" > > in other words create the class on the fly. >That''s not a good idea. The second time round you''ll be trying to (effectively) change the superclass of School which won''t work (it might work in development because of class reloading. In production definitely not). Fred> Then in the controller code, etc, you just do the usual > School.find.... > > > Stephan > -- > 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?hl=en -~----------~----~----~----~------~----~------~--~---