On Friday, August 7, 2015 at 7:42:19 AM UTC-7, Sylver wrote:
>
> Hello,
>
> I'd like to find a way to automagically extend data a model dataset 
> returns with some related eager loaded table.
>
> I have a model as this (simplified) :
>
> class Foo < Sequel::Model
>   many_to_one :bar, :select => [:id, :field1, :field2]
> end
>
>
> And I'd like to retrieve the data like this :
>
> foo_set = Foo.dataset.eager(:bar).all
> # => [{:id => 1, :bar => {:id => 32, :field1 => "value", :field2 => 
> "value"}}, ...]
>
>
That's not how eager loading works in Sequel.  Eager loading prepopulates 
associations, it doesn't add additional keys to the resulting value hashes. 
Also, you would get model objects, not plain hashes:

The most similar thing to what you want is using the graph_each extension. 
 But that splits the hashes into subhashes per table, and will give you 
something like:

  [{:foos=>{:id=>1}, :bar=>{:id => 32, :field1 => "value", :field2 => 
"value"}]

I think the easiest way to get what you want would be to use eager, but 
merge the associations into the values:

  Foo.dataset.eager(:bar).all{|foo| foo.values.merge!(foo.associations)}

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