I have the following variable definition:
@monday = (Time.now).at_beginning_of_week
I use that in different models, controllers, and views.
Where can I define it (and how should I define it — @@? ) so I can define it once and use throughout my Rails application?
Should it be in environment.rb? Should it be a @@?
I would add it to application controller:
So it will be accessible in all of yours controllers and views. If you want to use it in your models, then probably you need it as some param, on example for scopes. Then your controller is place where you should pass this variable to model:
I don’t think you need to have only one instance of this variable for whole application. Initializing it is quite simple. Also it is not good when you initialize it and don’t use it. For me it is hard to imagine that you need it in all of your controllers and actions.
The other way is you can define class:
And put it in
config/initializers(probably there is better place where to put it). Then you can access it from anywhere in your application: