Reputation: 6278
If I have the following code in a class method defined on the association. Will that value then get cached or is that just for the scopes?
def for_today
where 'planned_for = ?', Time.zone.now
end
To clarify, if I do the below with a scope the Time.zone.now value will be the same as when the class was cached and I want to make sure this doesn't happen for the above or how to work around it.
scope :for_today, where('planned_for = ?', Time.zone.now)
So the correct way of writing a scope that is using a dynamic value would be to use a lambda like
scope :for_today, lambda { where('planned_for = ?', Time.zone.now) }
Do I need to do something similar for a class or instance method and if that is so what exactly?
Upvotes: 0
Views: 139
Reputation: 1580
No, that value will not get cached for class and instance methods, so you can safely use them.
As a rule of a thumb: for simple conditions use scope
, and for more complex cases, use class methods.
Also, with default configuration, all times stored in database are in UTC, so you can just use Time.now since it will be auto-converted to UTC.
Upvotes: 1