where do I put my global enviroment variables?

Hi

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?

thanks for your help

Marc

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.

Rein

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

  has_attachment :storage => :file_system,     :content_type => :image,     :size => 0.bytes..2.megabytes,     :thumbnails => { :thumb => '120x120>' },     :processor => :MiniMagick

If I create a seperate model for the settings. How would I access that in here? Sorry for the newbie questions :slight_smile:

You can have a settings table (because you want the client or
operations people to be able to change it at run-time).

class Setting < ActiveRecord::Base end

The attributes would be in the migration for name, value, etc.

In the other model/controller/view simply do something like
Setting.find_by_name("max_image_size").value to get at the setting
value.

Michael

thanks for your answers

<snip>

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.

The bug tracking system Retrospectiva <http://www.retrospectiva.org> uses a YAML based general settings and configuration file which is managed by a configuration manager class. Read the following code for more information: <GitHub - dim/retrospectiva: Open source, web-based agile project management, featuring: Goal planner, Story management, Issue-tracker, Code review, SCM Integration, Wiki & Blog. It is intended to assist the collaborative aspect of work carried out by software development teams.;

Adam