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

On Sun, Apr 29, 2012 at 2:43 PM, Jake K <[email protected]> wrote:
> 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 <[email protected]> wrote:
>> Here's a starting hint:
>>
>> http://ar.rubyonrails.org/classes/ActiveRecord/Calculations/ClassMeth...
>>
>> If you need more help than a hint, don't be afraid to ask.
>>
>> Bryan
>>
>>
>>
>>
>>
>>
>>
>> On Sat, Apr 28, 2012 at 6:07 PM, Jake K <[email protected]> wrote:
>> > 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
>>
>> > --
>> > You received this message because you are subscribed to the Google Groups 
>> > "Hobo Users" group.
>> > To post to this group, send email to [email protected].
>> > To unsubscribe from this group, send email to 
>> > [email protected].
>> > For more options, visit this group 
>> > athttp://groups.google.com/group/hobousers?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Hobo Users" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/hobousers?hl=en.
>

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

Reply via email to