以下是我的模特:
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 =&gt; 23},{2 =&gt; 7},{n =&gt; X}}
基本上,对于第1小时,有23次互动,对于第2小时,有7次。我想每天1到24小时这样做。
交互有一个名为“interaction_time”的列,当发生交互时。感谢
答案 0 :(得分:6)
对于postgres,
Interaction
.select('EXTRACT(HOUR from interaction_time)')
.group('EXTRACT(HOUR FROM interaction_time)')
.order('EXTRACT(HOUR from interaction_time)')
.count
答案 1 :(得分:1)
所以......在你的交互模型中创建一个方法:
def hour
self.interaction_time.to_date.strftime('%H')
end
将从0-23返回您的模型。将它们分组:
@user = FbUser.find(params[:id])
hours_of_day = @user.interactions.group_by(&:hour)
立即播放该数组...... hours_of_day.keys
,hours_of_day.values ,
。size`等。
密钥应该是小时,然后该值应该是该小时内发生的不同交互的数组
hours_of_day.each do |key, value|
puts key
puts value.size
end
类似的......