Default values are cached on rails startup..
Altonymous opened this issue · 3 comments
Altonymous commented
The following code grabs the period when the rails service starts and caches them. Thus making it a static time period instead of a rolling one that corresponds to the request.
has_scope :by_period, using: [:started_at, :ended_at], type: :hash, default: { started_at: Time.now.utc.beginning_of_day - 14.days, ended_at: Time.now.utc.beginning_of_day }
rafaelfranca commented
Yes. This is because that code is executable when the class is loaded. If you need the default to be dynamic you need to use a proc.
rafaelfranca commented
has_scope :by_period, using: [:started_at, :ended_at], type: :hash, default: ->() { { started_at: Time.now.utc.beginning_of_day - 14.days, ended_at: Time.now.utc.beginning_of_day } }
Altonymous commented
That's what I thought. Thanks!