加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何按小时分组记录计数

发布时间:2020-12-17 02:46:43 所属栏目:百科 来源:网络整理
导读:这是我的模特: Interaction.rb class Interaction ActiveRecord::Base belongs_to :report belongs_to :post belongs_to :fb_user has_one :fb_page Report.rb class Report ActiveRecord::Base belongs_to :user has_many :page_stats,dependent: :destroy
这是我的模特:

Interaction.rb

class Interaction < ActiveRecord::Base

  belongs_to :report
  belongs_to :post
  belongs_to :fb_user
  has_one :fb_page

Report.rb

class Report < ActiveRecord::Base

  belongs_to :user

  has_many :page_stats,dependent: :destroy
  has_many :interactions,dependent: :destroy
  has_many :fb_users,through: :interactions
  has_one :fb_page,foreign_key: :fb_id,primary_key: :fb_page_id
  has_many :posts,dependent: :destroy

FbUser.rb

class FbUser < ActiveRecord::Base

  self.primary_key = "id"
  has_many :interactions

我想要做的是按照他们发生的一天中的小时进行交互.我正在寻找1-24(一天中的小时,UTC)的哈希值,并将交互计数按发生时间分组.

这是一个示例返回值:

{{1 => 23},{2 => 7},{n => X}}

基本上,对于第1小时,有23次互动,对于第2小时,有7次.我想每天1到24小时这样做.

交互有一个名为“interaction_time”的列,即交互发生时.谢谢

解决方法

对于postgres,

Interaction
.select('EXTRACT(HOUR from interaction_time)')
.group('EXTRACT(HOUR FROM interaction_time)')
.order('EXTRACT(HOUR from interaction_time)')
.count

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读