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?

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

Frederick Cheung wrote:

Lucas Campbell wrote:

Frederick Cheung 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)

Lucas Campbell wrote:

Lucas Campbell wrote:

Frederick Cheung 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

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