I'm not sure where in my script I would put my global enviroment
variables and when to load them.
I would like to create a database table that stores some applications
settings.
For example the maximum allowed size of an image that the user may
upload.
Where in the rails app would I load that data and where would I store
it so it is accessible in both the model and the controller?
This is not a global environment variable. This is business logic that
belongs in the model that handles your image uploads. attachment_fu
already provides a way to specify max upload size, for instance.
Otherwise:
class Image < ActiveRecord::Base
MAX_IMAGE_SIZE = 10.megabytes
def self.max_image_size; MAX_IMAGE_SIZE; end
end
It certainly doesn't belong in the controller or the view. Global
environment variables are a bad code smell in ruby/rails, mainly a
holdover from people with a PHP background, I think.
Jamal: If it is somehow related only to the presentation of
information then it may make sense to put it in a helper. It's always
best to encapsulate such things properly to separate concerns and
avoid polluting the namespace. Ruby/Rails makes this easy.
thanks for your answers.
You are right .. I come from a php background and back in the days I
always had my config.php file that had all the settings in it.
But what I'd like to do right now is load my settings from a table in
the database and then work with them.
Would I need to create a seperate settings model for that?
I wanna use the size property for example in the following model where
I define the max size of an image that the user can upload
class ArticleImage < ActiveRecord::Base
belongs_to :article
you may also want to use a .yml file and load this using your config
file, you cant change the settings at runtime but it will be a lot
better from the performance side of things
You can change settings at runtime by using YAML.dump to save your new
settings to the config file.