0

I have 2 models, weekly_report and consult_stat and they are organized as such:

class ConsultStat < ActiveRecord::Base
    belongs_to :weekly_report
end

class WeeklyReport < ActiveRecord::Base
    has_many :consult_stats
end

An attribute on my consult_stats table is :consults and I want to retrieve the total number of consults for a given set of weekly reports.

If I'm dealing with 1 weekly_report, I can do

WeeklyReport.find(x).consult_stats.sum(:consults)

however when I try to select a group of weekly_reports:

WeeklyReport.where("start_date > ?", "2016-11-01")

and retrieve the consult sum for their consult_stats using joins I keep getting an error.

Jeremy Thomas
  • 6,240
  • 9
  • 47
  • 92

1 Answers1

0

Thats to this post I realized that

  • weekly_report is singular in joins(:weekly_report) because it's a belongs_to relationship
  • weekly_reports is plural in where because it's the table name

making my query:

ConsultStat.joins(:weekly_report).where("weekly_reports.start_date > ?", "2016-11-01")
Community
  • 1
  • 1
Jeremy Thomas
  • 6,240
  • 9
  • 47
  • 92