On Monday, March 2, 2015 at 12:18:48 PM UTC-8, Andrew Burleson wrote:
>
> Sorry to ask a sort of newbie SQL question but I'm having a hard time 
> remembering how to do this and translating it into Sequel:
>
> If I have to models like this:
>
> class Apartment < Sequel::Model
>   one_to_many :residents
> end
>
>
> class Resident < Sequel::Model
>   many_to_one :apartment
> end
>
>
>
> How can I query all apartments ordered by resident count? I thought that 
> this might work...
>
> result = Apartment.eager_graph(:residents).order(:residents__count).all
>
>
> ...but that fails with a PG grouping error: "Sequel::DatabaseError: 
> PG::GroupingError: ERROR:  column "apartments.id" must appear in the 
> GROUP BY clause or be used in an aggregate function..."
>
> Thanks for the help!
>

Simplest way is probably using a subselect in the order clause:

  
Apartment.order(Resident.where(:apartment_id=>:apartments__id).select{count{}.*})

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 http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to