Calculate average for parent based on children

12 views
Skip to first unread message

Jake K

unread,
Apr 28, 2012, 6:07:45 PM4/28/12
to Hobo Users
Hi all,

I'm making a review application, and am working with two kinds of
items - "books" and "reviews", where books have many reviews and
reviews are children of books. Each review has a few integer fields
where reviewers can rate some quality of the book. How would I go
about calculating an average value for some question from all the
reviews, and display it on the book's view page? Essentially I'm
trying to show the average star rating for a book based on all the
star values in its children reviews.

Thanks for any advice!
-Jake

Bryan Larsen

unread,
Apr 29, 2012, 9:22:32 AM4/29/12
to hobo...@googlegroups.com
Here's a starting hint:

http://ar.rubyonrails.org/classes/ActiveRecord/Calculations/ClassMethods.html

If you need more help than a hint, don't be afraid to ask.

Bryan
> --
> You received this message because you are subscribed to the Google Groups "Hobo Users" group.
> To post to this group, send email to hobo...@googlegroups.com.
> To unsubscribe from this group, send email to hobousers+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/hobousers?hl=en.
>

Jake K

unread,
Apr 29, 2012, 2:43:14 PM4/29/12
to Hobo Users
Hi Brain,

Thanks for the hint! Is it possible to calculate the average of a star
rating field, for example, from all of the reviews (children) of a
book (parent)? Or would this only work if I were trying to calculate
an average of some field within a single model? If it's possible to
calculate this for a parent based on all its children, would I want to
put the calculation in the child model or the parent model?

Thanks,
Jake

On Apr 29, 9:22 am, Bryan Larsen <br...@larsen.st> wrote:
> Here's a starting hint:
>
> http://ar.rubyonrails.org/classes/ActiveRecord/Calculations/ClassMeth...

Bryan Larsen

unread,
Apr 29, 2012, 3:14:51 PM4/29/12
to hobo...@googlegroups.com
It should be fairly easy. For instance, you can add this to your book model:

def rating
reviews.average('rating')
end

assuming that the column on the review model is called rating.

Bryan

Jake K

unread,
Apr 29, 2012, 3:30:51 PM4/29/12
to Hobo Users
Ah, got it. Then how would one display that on a custom show view? I
tried <% this.rating %> and <% book.rating %> but that doesn't seem to
do the trick... Sorry for the newb question!

Thanks,
Jake

Bryan Larsen

unread,
Apr 29, 2012, 3:36:52 PM4/29/12
to hobo...@googlegroups.com
On Sun, Apr 29, 2012 at 3:30 PM, Jake K <jkus...@me.com> wrote:
> Ah, got it. Then how would one display that on a custom show view? I
> tried <% this.rating %> and <% book.rating %> but that doesn't seem to
> do the trick... Sorry for the newb question!

<%= this.rating %> or <view:rating />

Bryan

Jake K

unread,
Apr 29, 2012, 3:39:13 PM4/29/12
to Hobo Users
Ahh, that = sign. That worked, thanks so much Bryan!

On Apr 29, 3:36 pm, Bryan Larsen <br...@larsen.st> wrote:

kevinpfromnm

unread,
Apr 30, 2012, 12:53:41 PM4/30/12
to hobo...@googlegroups.com
yes:

    <% run this ruby code, no output %>

    <%= run this ruby code and output whatever is returned as a string with to_s %>
Reply all
Reply to author
Forward
0 new messages