Thank you for the response, Jeremy!

I had good success using group_concat in order to collect my tour guide 
names, but the answer that I settled on had been staring me in the face the 
whole time.

I needed the output as a JSON object, and I was already using the 
json_serializer plugin.

Using that plugin makes this a trivial matter, simply:

# Make sure to add the plugin to your model
Sequel::Model.plugin :json_serializer

Tour.to_json(:include => {:guides => {:only => :guide_name}})



On Wednesday, April 15, 2015 at 3:20:43 PM UTC-4, Jeremy Evans wrote:
>
>
> With PostgreSQL, this should be fairly easy using array_agg:
>
> DB[:tours].left_join(:tour_guides, :tour_id=>:id).
>   left_join(:guides, :id=>:guide_id).
>   select_group(:tours__id).
>   select_append{array_agg(:guides__guide_name).as(:guide_name)}
>
>
> If you are unfortunate enough to be using something else, you could try 
> Dataset#to_hash_groups, but that will give you a slightly different result:
>
> DB[:tours].left_join(:tour_guides, :tour_id=>:id).
>   left_join(:guides, :id=>:guide_id).
>   select_hash_groups(:tours__id, :guides__guide_name)
> # => {1=>["Elanor", "Test"]}
>
> 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