Rails:默认范围是由查询缓存缓存的吗? [英] Rails: default scoping being cached by query cache?

查看:64
本文介绍了Rails:默认范围是由查询缓存缓存的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了这样的默认范围,它是动态的:

I got a default scoping like this which is dynamic:

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

当我使用此代码时,第一天还可以。可以说第一天是2011年3月28日

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

,但是第二天似乎仍在使用出发日期> = 28-03-2011

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

是否缓存了我的默认作用域?

Is my default scoping being cached?

推荐答案

问题在于,仅在加载应用后一次执行该代码,因此实际日期没有更改。您需要更改它以延迟加载:

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

这种方式, Datetime.current.beginning_of_day 会在您每次查询时进行评估。

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

这篇关于Rails:默认范围是由查询缓存缓存的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆