Rails: default scoping being cached by query cache?

本秂侑毒 提交于 2019-12-07 15:15:32

问题


I got a default scoping like this which is dynamic:

default_scope :conditions => ["departure_date >= ?", DateTime.current.beginning_of_day]

When I use this code the first day is ok. Lets say first day is 28-03-2011

But the next day seems like it's still using "departure_date >= 28-03-2011"

Is my default scoping being cached?


回答1:


The problem is that that code is only being executed once, when your app is loaded, and thus the actual date isn't changing. You need to change it to load lazily:

default_scope lambda { { :conditions => ["departure_date >= ?", DateTime.current.beginning_of_day] } }

This way, Datetime.current.beginning_of_day will be evaluated each time you make a query.



来源:https://stackoverflow.com/questions/5257452/rails-default-scoping-being-cached-by-query-cache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!