Dave
Dave

Reputation: 1175

How can I add the values of integer fields across records and divide by the number of records?

I have a table of review records where one of the fields (rating, a number 1 to 5) is an integer and they are displayed as partials on their assigned venue show page.

How can I show on the venue show page the total of all the ratings from all its reviews divided by the number of reviews it has?

I get the total number of reviews written for the venue with:

 <%= @venue.reviews.count %>

Thanks for any help its much appreciated!

Upvotes: 1

Views: 73

Answers (1)

jdl
jdl

Reputation: 17790

@venue.reviews.average(:rating)

Upvotes: 2

Related Questions