每天显示用户对所有任务花费的时间 [英] To display time spent by user on all tasks daily

查看:127
本文介绍了每天显示用户对所有任务花费的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一个用户在一天内对所有任务花费多少时间。

I want to display how much time a single user spends on all tasks in a day.

目前我正在使用此查询来显示所有用户花费的时间每天的所有任务的用户:

Currently I'm using this query to display the time spent by all the users on all tasks daily:

TaskTimeTracker.group("year(created_at)").group("month(created_at)").group("day(created_at)").sum("time_spent")

我使用的模型信息。

#Table name: tasks

#id              :integer          not null, primary key
#name            :string(255)
#description     :text(65535)
#due_date        :string(255)
#status          :integer
#priority        :integer
#project_id      :integer
#user_id         :integer
#created_at      :datetime         not null
#updated_at      :datetime         not null
#estimated_time  :datetime
#start_time      :datetime
#completion_time :datetime
#parent_task_id  :integer
#task_type       :string(255)
#author_id       :integer
#slug            :string(255)

#Indexes

#index_tasks_on_project_id  (project_id)
#index_tasks_on_slug        (slug) UNIQUE
#index_tasks_on_user_id     (user_id)

#Table name: task_time_trackers

#id          :integer          not null, primary key
#description :text(65535)
#time_spent  :float(24)
#created_at  :datetime         not null
#updated_at  :datetime         not null
#task_id     :integer
#created_by  :string(255)

#Indexes

#index_task_time_trackers_on_task_id  (task_id)


class TaskTimeTracker < ActiveRecord::Base
    belongs_to :task
    validates :description,presence:true
    validates :time_spent,presence:true
end

class Task < ActiveRecord::Base
  include PublicActivity::Model
  attr_accessor :estimated_time_field, :prev_user_id
  validate :due_date_validation

  extend FriendlyId
  friendly_id :task_type, use: [:slugged,:finders]

  belongs_to :project
  belongs_to :user
  has_many :worked_tasks, dependent: :destroy
  has_many :comments, dependent: :destroy
  has_many :logs, dependent: :destroy
  has_and_belongs_to_many :tags

  has_many :subtasks, class_name: "Task", :foreign_key => :parent_task_id
  belongs_to :parent_task, class_name: "Task" 
  has_many :task_time_trackers, dependent: :destroy
end


推荐答案

这是我能够得到所需的结果

This is how I was able to get the desired result

u_id = params [:u_id]

u_id = params[:u_id]

TaskTimeTracker.joins(:task).where( task_time_trackers: { user_id: u_id}).where("(task_time_trackers.created_at BETWEEN ? AND ?)",Date.current.beginning_of_month, Date.current.end_of_month ).group("task_time_trackers.task_id").group("DAY(task_time_trackers.created_at)").sum("time_spent")

它将创建一个HASH。

It will create a HASH.

这篇关于每天显示用户对所有任务花费的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆