variables - Where should I store calculated values I use throughout my application in Rails? -
i have following variable definition:
@monday = (time.now).at_beginning_of_week
i use in different models, controllers, , views.
where can define (and how should define -- @@? ) can define once , use throughout rails application?
should in environment.rb? should @@?
i add application controller:
before_filter :load_date def load_date @monday = (time.now).at_beginning_of_week end
so accessible in of yours controllers , views. if want use in models, need param, on example scopes. controller place should pass variable model:
@models = mymodel.before_date(@monday)
i don't think need have 1 instance of variable whole application. initializing quite simple. not when initialize , don't use it. me hard imagine need in of controllers , actions.
the other way can define class:
class mydate def self.get_monday time.now.at_beginning_of_week end end
and put in config/initializers
(probably there better place put it). can access anywhere in application:
mydate::get_monday
Comments
Post a Comment