3

I have a left outer join query and I would like to nest the result when I execute the query from the .all method. At the moment I am only able to return the result in a flat hash but I would like a nested hash so the join table name is the next level in the hash. Is this even possible within Sequel?

I have product and meta tables and the Sequel command is via graph so the columns are aliased, and look like this:

db[:product].graph(:meta, {product_id: : product_id}, {join_type: :left_outer}).all

What's annoying with the graph command is it will only alias columns if it matches the main table. Is there a way of making sure it changes all the columns as you'll see that the quantity column hasn't got a prefix of meta_?

If this is possible then I suppose I could modify the hash manually by moving the key => values into a separate hash that is nested by matching the prefix (meta_) of the keys.

This is what is currently being returned but I would like the nested hash instead:

Currently:

{
  id: 1,
  name: 'Shave',
  product_id: 123,
  meta_id: 1,
  meta_product_id: 123,
  quantity: 2
}

What I'd like to see:

{
  id: 1,
  name: 'Shave',
  product_id: 123,
  meta: {
    id: 1,
    product_id: 123,
    quantity: 2
  }
}
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Peter
  • 749
  • 10
  • 27

1 Answers1

0

I was having the same issue and found this question via Google. It had me stumped for awhile but I found this GitHub issue which mentions that the functionality was moved to the graph_each extension. This extension is included with the Sequel gem, but it is not enabled by default. You can enabled it for all datasets using DB.extension(:graph_each). The docs page has more information.

Bob Nadler
  • 1,247
  • 14
  • 18