heartcombo/has_scope

Default values are cached on rails startup..

Altonymous opened this issue · 3 comments

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 }

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.

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 } }

That's what I thought. Thanks!