Comparing identical DateTime objects in ruby — why are these two DateTime.now's not equal?

后端 未结 4 922
后悔当初
后悔当初 2021-01-13 12:55

In a rails app, I have model code of the form:

def do_stuff(resource)
  models = Model.where(resource: resource)
  operated_at = DateTime.now

  models.each         


        
4条回答
  •  耶瑟儿~
    2021-01-13 13:35

    I assume that this problem is due to the lazy scope that you use: models = Model.where(resource: resource)

    models is a proxy collection and will be resolved by rails at some point and might be re-evaluated without you knowing exactly.

    so when you change an attribute and you don't reload the object before checking a property it might not be up to date.

提交回复
热议问题