On Wednesday, September 20, 2017 at 5:11:20 AM UTC-7, Satyanarayana Gandham
wrote:
>
> Hi,
>
> We have a model called "Item". The Item is associated to UserReview Model
> and CriticReview Model. Both UserReview and CriticReview models have a
> column called rating. I want to find the item having the maximum average
> rating. The average rating is the average of rating column in both user
> reviews and critic reviews. So, I tried the following query:
>
> Item.max { UserReview.where(item_id:
> Sequel[:items][:id]).union(CriticReview.where(item_id:
> Sequel[:items][:id])).select{avg(:rating)} }
>
> But, I am getting the following error: Sequel::DatabaseError:
> Mysql::Error: Unknown column 'items.id' in 'where clause'
>
> How will I be able to calculate it?
>
This is really an SQL question, not a Sequel question. You are producing
SQL that is invalid, since the items table is not valid in the context you
are using it. You probably want to join items to both parts of the union,
then call max on the unioned dataset.
Thanks,
Jeremy
--
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.