I believe you can use `select_append` with a subquery to return a column 
representing the average rating.

Something like:

    Item.select_append { Review.where(item_id: Sequel[:item][:id]).avg(:rating) 
}

You’ll have to access this field using the Array-style accessor, as it won’t 
exist as a method on the Item instances.

> On Jun 26, 2017, at 10:41 AM, Satyanarayana Gandham 
> <[email protected]> wrote:
> 
> Hi,
> 
> I have two models Item and Review. Item has one_to_many association with 
> Review. Review has rating column which stores integers from 1 to 10. Rating 
> of an item equals avg of the rating column in item's reviews_dataset.
> 
> When I list the items, I want to list the rating as well. But the issues here 
> is that I am calling DB 'N' no. of times for each item. Basically a N + 1 
> problem. I have used eager loading for the associations to solve the N +1 
> issue for the associations. But, the average here is not an association. Is 
> there a way I can use eager functionality (or some other way) to load the 
> average of a column in the associated dataset and not face the N +1 issue.
> 
> Thanks,
> Satya
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sequel-talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected] 
> <mailto:[email protected]>.
> To post to this group, send email to [email protected] 
> <mailto:[email protected]>.
> Visit this group at https://groups.google.com/group/sequel-talk 
> <https://groups.google.com/group/sequel-talk>.
> For more options, visit https://groups.google.com/d/optout 
> <https://groups.google.com/d/optout>.

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to