订阅

订阅

问题描述:

我有三个字段activestart_suscriptionend_suscriptionactive is booleanF IT是真实的,因为订阅未完成,我的问题是如何使导轨自动将其更改为模型假'订阅结束时,在django这很容易但我只是开始与轨道,我不知道。订阅

,我建议你不要使用另一列到“缓存”,它可以很容易地计算出一个信息:

如果删除列active,你可以决定是否一个记录是活动的定义实例方法active?

# in your model 
def active?(as_of_date = Date.current) 
    (start_suscription..end_suscription).include?(as_of_date) 
end 
# usage 
instance = YourModel.new(start_suscription: Date.yesterday, end_suscription: Date.tomorrow) 
instance.active? # => true 
instance.active?(Date.current + 1.week) # => false 

要获得所有活动记录,做一个范围:

scope :active, -> (as_of_date = Date.current) { where('? BETWEEN start_suscription AND end_suscription', as_of_date) } 
# usage 
YourModel.active 
YourModel.active(Date.yesterday) 

如果您想坚持使用缓存列(我强烈建议您不要),您需要一名工作人员(每X时间触发一次)抓取所有现在处于非活动状态的记录,并更新其active列。

如果活动是数据库字段,你可以在你的订阅模式创建一个回调像

before_save :set_active 

def set_active 
self.active = self.start_subscription >= Date.today and self.end_suscription <= Date.today 
end 

像吉井先生说:积极不应该是一个数据库字段。如果是,并且你不想改变它,你应该在每个请求或日常任务中触摸记录。

+0

因此,如果我将来做了一个备用,那么即使它尚未启动,我也会将“active”属性设置为“active” – MrYoshiji