开发者

Rails: saving duration between "events"

开发者 https://www.devze.com 2023-04-11 12:27 出处:网络
Long story short, each time a user views a resource, an Event model object is created.Each event has a duration column.How can I best save the duration between each event?

Long story short, each time a user views a resource, an Event model object is created. Each event has a duration column. How can I best save the duration between each event?

class Event
  belongs_to :session
end

My gut reaction was to use an after_save callback each time a new event was created, calculate the time between "now" and the previous event, and save that 开发者_开发百科as the previous event's duration. However, this creates a "waterfall" effect, as the callback is then executed as the previous event is saved, and then the previous before that, and so on.

Any suggestions?

Thanks!


You'd better use after_create callback, this will not trigger while updating previous element


I'd probably keep the last event time in the user's session, because it'd be fast... Whether or not this makes sense may depend on when you need this info, though--is it for reporting, used during interaction, etc.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号