3
我在after_save回调中更新模型实例时使用update_attributes时遇到问题。 Update_attributes返回true,但属性没有在模型实例中设置。update_attributes无法更新after_save回调中的对象(mongomapper + rails3)
模型对象Graph有许多数据点,我想跟踪最大值以及何时测量。由于各种原因,我想这种非规范化的信息,所以我有下面的代码:
class Graph
include MongoMapper::Document
many :datapoints, :dependent=>:destroy
key :max_value, Float
key :max_value_at, Time
end
,并在我的数据点:
class Datapoint
belongs_to :graph
key :graph_id, ObjectId, :required=>true
key :value, Float
key :time, Time
after_save :update_max_on_save
....
def update_max_on_save
g = self.graph? ? self.graph : Graph.find_by_id(self.graph_id)
if g.max_value.nil? || g.max_value < self.value
g.update_attributes({:max_value=>self.value, :max_value_at=>self.time})
end
end
end
任何人都能够揭示为什么这种方法更新一些光图的属性会失败?