On Saturday, January 10, 2015 at 3:25:11 PM UTC-8, Horacio Peña wrote:
>
> Hi!
>
> From 
> http://sequel.jeremyevans.net/rdoc/classes/Sequel/Model/Associations/DatasetMethods.html
> :
>
> eager_graph (*associations)
>
> This method uses Dataset#graph to create appropriate aliases for columns 
> in all the tables. Then it uses the graph's metadata to build the 
> associations from the single hash, and finally replaces the array of hashes 
> with an array model objects inside all
> I understand that if I do Model.eager_graph(:association). ... .first I 
> should get a Model instance, but I'm getting a Hash, as if I'm were using 
> .ungraphed
>
> 2.0.0-p247 :013 > Suc::Asignacion.eager(:tarea).first
> #<Suc::Asignacion @values={:id=>220, :tarea_id=>3, :posicion=>1, 
> :usuario_id=>3102, :desde=>2000-01-01 09:00:00 -0300, :hasta=>2000-01-01 
> 13:00:00 -0300}>
>
> 2.0.0-p247 :014 > Suc::Asignacion.eager_graph(:tarea).first
> {
>              :id => 220,
>        :tarea_id => 3,
>        :posicion => 1,
>      :usuario_id => 3102,
>           :desde => 2000-01-01 09:00:00 -0300,
>           :hasta => 2000-01-01 13:00:00 -0300,
>      :tarea_id_0 => 3,
>           :fecha => #<Date: 2014-12-23 ((2457015j,0s,0n),+0s,2299161j)>,
>     :tarea_desde => 2000-01-01 09:00:00 -0300,
>     :tarea_hasta => 2000-01-01 13:00:00 -0300,
>        :cantidad => 20,
>     :campanya_id => 2456,
>           :casos => 10
> }
>
>   class Asignacion < Sequel::Model(:suc_asignacion_tareas)
>     many_to_one :tarea
>   end
>
>   class Tarea < Sequel::Model(:suc_tareas)
>     one_to_many :asignaciones, class: :Asignacion
>   end
>
> What am I missing?
>

The documentation for both #eager and #eager_graph specifically state that 
you need to call #all on the dataset, and you aren't doing that (you are 
calling #first, which calls #each).  If you change your code to call 
.all.first instead of .first, that should fix it. 

Note that in general you don't want to eager load if you are only loading a 
single object.  Eager loading only really makes sense if you are loading 
multiple objects and want to preload the associated objects for all of the 
objects.

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