Likely my syntax (or your version of Sequel?), as I didn’t have a chance to
confirm.
You can try symbol splitting, or maybe Sequel.qualify
Item.select_append { Review.where(item_id: :items__id).avg(:rating) }
or
Item.select_append { Review.where(item_id: Sequel.qualify(“items”,
“id”)).avg(:rating) }
I noticed I passed a singular version of the table name originally so it likely
wouldn’t of worked anyways.
> On Jun 27, 2017, at 11:09 AM, Satyanarayana Gandham
> <[email protected]> wrote:
>
> Hi Adam,
>
> Thanks for the reply. Unfortunately, I am getting an error. '[]' is not
> defined for Sequel:Module.
>
> Regards,
> Satya
>
> On Monday, June 26, 2017 at 9:21:24 PM UTC+5:30, Adam Daniels wrote:
> 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] <javascript:>> 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] <javascript:>.
>> To post to this group, send email to [email protected] <javascript:>.
>> 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]
> <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.