私は以下のエンティティを持っています:
User has_many Projects
Project has_many ProjectKeywords
Keyword has_many ProjectKeywords
Project and Keyword has_many Reports
reports
ページの 1 つにデータ チャートを表示したいのですが、Projects
が所有するの を表示する方法がわかりませんUser
。これは、Result
テーブルから行を表示する方法です。
Reports controller:
def self.chart_data(start = 1.weeks.ago)
total_possitions = possition_by_day(start)
possitions_top_fiftys = where("possition < ?", 50).possition_by_day(start)
(start.to_date..Date.today).map do |date|
{
created_at: date,
possitions_top_fifty: possitions_top_fiftys[date] || 0,
}
end
end
def self.possition_by_day(start)
results = unscoped.where(created_at: start.beginning_of_day..Time.zone.now)
results = results.group("date(created_at)")
results = results.order("date(created_at)")
results = results.select("date(created_at) as created_at, count(*) as count")
results.each_with_object({}) do |result, possitions|
possitions[result.created_at.to_date] = result.count
end
終わり