On Tuesday, June 2, 2015 at 8:27:54 AM UTC-7, [email protected] wrote:
>
> require 'sequel'
>
> DB = Sequel.sqlite
>
> DB.create_table :artists do
>   primary_key :id
>   String :name
> end
>
> DB.create_table :albums do
>   primary_key :id
>   String :name
> end
>
> DB.create_table :artists_albums do
>   foreign_key :artist_id, :artists
>   foreign_key :album_id, :albums
> end
>
> DB[:artists].insert name: 'Artist Foo'
>
> DB[:albums].insert name: "Foo's Album (Part 1)"
> DB[:albums].insert name: "Foo's Album (Part 2)"
> DB[:albums].insert name: "Foo's Album (Part 3)"
>
> DB[:artists_albums].insert artist_id: 1, album_id: 1
> DB[:artists_albums].insert artist_id: 1, album_id: 2
> DB[:artists_albums].insert artist_id: 1, album_id: 3
>
>
> Having these, which is the easiest way to obtain
>
>   {id: 1, name: "Artist Foo", albums: [{id: 1, name: "Foo's Album (Part 
> 1)"}, {id: 2, name: "Foo's Album (Part 2)"}, {id: 3, name: "Foo's Album 
> (Part 3)"}]}
>

Without using models, the easiest way is probably:

hash = DB[:artists][:id=>1]
hash[:albums] = DB[:albums].select_all(:albums).join(:artists_albums, 
:album_id=>:id).where(:artist_id=>hash[:id]).all
hash

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