I have three models: Amod, Bmod and Cmod.
Amod has_many
Bmods, Bmod has_many
Cmods.
Modb has a column called “integer” (type: integer) and Modc has a column called “date” (type: date).
I am using Chartkick to chart my data, which takes an hash (or array) as an argument.
What I am trying to do is:
- For a given record in Amod, return a hash where the keys are made up of each distinct ‘date’, and the values are made up of the the ‘integers’ that have that date.
- I would like to generate three variants of this hash: one that shows the sum of all integers with a given date, one that shows the mean of all integers with a given date, and one that shows the total number of integers with a given date.
Here is what I have:
mybmod = Bmod.where(amod_id: Amod.first.id) # Seems to work fine
myjoin = mybmod.joins(:cmods).select("cmods.date, bmods.integer") # Seems to work fine
mygroup = myjoin.group(:date) # Seems to work fine
myhash = mygroup.sum # Returns all values as 0
myhash = mygroup.count # Returns an error
Any ideas where I am going wrong here?