I'm using this code in my controller to access the average paper_weight
for all my users in the Heavy
classification.
if User.where(industry_type: 'Heavy')
.joins(:papers)
.where(papers: { paper_type: 'Officepaper' } )
.pluck('avg(paper_weight)').first.nil?
@heavy_indust_officepaper == 0
else
@heavy_indust_officepaper = User.where(industry_type: 'Heavy')
.joins(:papers)
.where(papers: { paper_type: 'Officepaper' } )
.pluck('avg(paper_weight)').first * 3
end
Then the variable @heavy_indust_officepaper
is displayed in a view.
The problem is that this code above doesn't seem to be calculating the average correctly when one or more users have a nil
entry in paper_type: 'Officepaper'
.
I know that because I have two users in the industry_type: 'Heavy'
One of them has one entry of paper_type: 'Officepaper'
which is the decimal 30.
And the other user has nil
entry in paper_type: 'Officepaper'
.
By my understanding the calculation should be 30 + 0 / 2(users) = 15
and then the 15
is multiplied by 3
which should give 45
But instead the variable @heavy_indust_officepaper
is displaying 90
in the view... which must be the result of 30 * 3
Is there a way to convert the nil
to 0
with in the code above???
Please Can some one advise me?
Here is a link to a question I asked earlier to day, I got help with this code from it Using .average with .pluck in ruby on rails app?