views:

21

answers:

2

Are models available within the modules. Can I access constants stored in a model class within a model.

I tried require 'modelfilename.rb' and tried to use the constants by Model::Constant_name but its unable to get the value. Since the constants are related to the model i store them with model and I access them within the module using the model I require within my module.

Thanks in advance.

A: 

Where is your module located ?
Any module located inside a rails application (the "lib" directory or a plugin for example) and accessed by that rails application has all the models accessible.
So you don't need to require them. You only need to call them.

If your module isn't inside your rails application, you need to define your models directory and load the one you require.

$:.unshift 'path/to/your/models/directory'
require 'modelfilename'

You need to first define in which directory your models are so that ruby knows what to load.
Then you require your model. The ".rb" extension isn't required.

Damien MATHIEU
A: 

You could use something like:

modelObject = Object.const_get(StackOverflow)

where StackOverflow is your model. This should work but I have a strong suspicion that ActiveRecord handles the calls to Object.const_get. In that case, just go with what Damien has suggested.

andHapp