
Note: when we define constants above, often we want to freeze the array. # accessible as a top-level constant this time # put this into config/initializers/my_constants.rb This is probably the best place, if your colours are really global and used in more than one model context. You could also create global constants which are accessible from everywhere in an initializer like in the following example. If you have true a truly unchangeable constant, you can also define it on the model class: class Card < ActiveRecord::Base The two options above allow you to change the returned array on each invocation of the accessor method if required. This is however discouraged as class variables might act kind of surprising with inheritance and in multi-thread environments. You can create class methods to access them without creating a new object instance: class Card < ActiveRecord::BaseĪlternatively, you can create class variables and an accessor. If your model is really "responsible" for the constants you should stick them there. Is there just no way to define anything to be accessible both from models and from views? I mean, I know models and views should be separate, but surely in some domains there'll be times they need to refer to the same domain-specific knowledge?

A method in application_helper.rb - this is what I'm doing so far, but the helpers are only accessible in views, not in models.A method on the model, something like def colours end - same problem.But I couldn't find a sane way to define it so that I can write in my views Card.COLOURS rather than something kludgy like. Constant class variables in the model.rb file that they're most associated with, such as =.Where can I define it in just one place so that it's accessible? This is used all over the place, in both models and views. To take a specific example, I want a constant COLOURS =. I cannot find any DRY place to put these definitions where they're available both to all my models and also in all my views.

In particular, they apply both in the logic of my models, and in the decisions taken in my views. I'm wanting to find a good place to stick definitions of truly global constants, that apply across my whole app. I've got a bunch of different models, views, controllers, and so on. I'm just getting started with my first Ruby on Rails webapp.
