Reputation: 7269
I'm working on an app that will track the worked time from users, and then, generate graphics from this data...
Having in mind that they can start any task today and done it tomorrow, I can't save an Date
...
By now, I'm saving an integer field like hours
and a integer minute
...
Is there a way (some gem, maybe) to help me dealing with this kind of problem?
Or any other approach?
Thanks
My class Atendimento
that contains all necessary data
class Atendimento < ActiveRecord::Base
attr_accessible :horas, :minutos, :empresa, :tipo_atendimento, :observacao, :empresa_id, :tipoatendimento_id
belongs_to :empresa
belongs_to :tipo_atendimento, :foreign_key => 'tipoatendimento_id'
belongs_to :usuario
validates :horas, :minutos, presence: true
end
Upvotes: 0
Views: 79
Reputation: 160
Instead of three fields you should add the starting and ending times as timestamps. You can do it by adding these lines to the migration file:
add_column :tasks, :started_at, :timestamp
add_column :tasks, :ended_at, :timestamp
In the model you can then use these timestamps for calculations.
class Task < ActiveRecord::Base
attr_accessible :started_at, :ended_at
validates :started_at, presence: true
validates :ended_at, presence: true
# Returns the duration of the task in seconds
def duration
ended_at - started_at
end
end
Upvotes: 1
Reputation: 51707
I would add two datetime columns to your database to track the start and stop timestamps of the event. Then you can just subtract the two values to get the time elapsed.
Upvotes: 1