I'm using Sequel to read rows from a SQLite database. No writing is necessary.
I am using a basic join. Consider this:
db = Sequel.sqlite
db[:user].join_table(:left, :photos, user_id: :user_id)
This joins the photo table on the user table (keeping the users that have no photos).
However this multiplies all the lines of the user by the number of its photos:
[
{user_id: 1, name: 'User1', photo_id: 1, photo_name: 'profile.jpg'}
{user_id: 1, name: 'User1', photo_id: 2, photo_name: 'cat.jpg'}
{user_id: 1, name: 'User1', photo_id: 3, photo_name: 'dog.jpg'}
{user_id: 2, name: 'User2', photo_id: 4, photo_name: 'profile.jpg'}
{user_id: 2, name: 'User2', photo_id: 5, photo_name: 'profile.jpg'}
{user_id: 3, name: 'User3', photo_id: nil, photo_name: nil}
]
I would like to have all the photo attributes nested under one key, like this:
[
{user_id: 1, name: 'User1', photos: [{photo_id: 1, photo_name: 'profile.jpg'}, {photo_id: 2, photo_name: 'cat.jpg'}, {photo_id: 3, photo_name: 'dog.jpg'}]}
{user_id: 2, name: 'User2', photos: [{photo_id: 4, photo_name: 'profile.jpg'}, {photo_id: 5, photo_name: 'profile.jpg'}]},
{user_id: 3, name: 'User3', photos: [] }
]
How can I achieve this "mini-relation"? Do I have to specify a model?