【发布时间】:2016-11-18 17:16:59
【问题描述】:
我想显示单个用户在一天内花费在所有任务上的时间。
目前我正在使用此查询来显示所有用户每天在所有任务上花费的时间:
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
【问题讨论】:
-
您需要获取单个用户完成的全部任务,而不是该时间的总和
-
您能否也添加您的模型以及您在什么情况下使用此查询
-
我正在将此查询用于时间表。
-
查看您的模型仍然非常有用。你也有作者表/模型。也请发帖
-
不,没有作者表或模型
标签: mysql ruby-on-rails ruby datetime