Rails:默认范围被查询缓存缓存?
我得到了这样的默认范围,它是动态的:
default_scope :conditions => ["departure_date >= ?", DateTime.current.beginning_of_day]
当我使用此代码时,第一天就可以了。假设第一天是 28-03-2011
但第二天似乎仍在使用 "departure_date >= 28-03-2011"
我的默认范围是否被缓存?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是,当您的应用程序加载时,该代码仅执行一次,因此实际日期不会改变。您需要将其更改为延迟加载:
这样,每次查询时都会评估
Datetime.current.beginning_of_day
。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:
This way,
Datetime.current.beginning_of_day
will be evaluated each time you make a query.