Rails: saving duration between "events"
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.
精彩评论