On Tuesday, July 21, 2015 at 3:59:37 AM UTC-6, Lin Jen-Shin wrote:
>
> I am trying to use use_cursor with eager, 
> but use_cursor requires to use each, 
> which didn't work well with eager. Any advise? 
>
> Thanks! 
>
> For now, I am doing something like this to 
> simulate it: 
>
> query.use_cursor(:rows_per_fetch => 100).each_slice(100) do |records| 
>   assocs = Assoication.where(:id => records.map(&:association_id)).all 
>   records.each do |r| 
>     r.association = assocs.find{ |a| a.id == r.association_id } 
>   end 
> end 
>
> It would be great if there's a better way to do this. 
>
> p.s. use_cursor: 
>
> http://sequel.jeremyevans.net/rdoc-adapters/classes/Sequel/Postgres/Dataset.html#method-i-use_cursor
>  
>

This isn't specific to or even really related to #use_cursor, it's that 
eager loading doesn't work with Dataset#each, it's handled inside 
Dataset#all. In order to eagerly load associated records, you need to have 
all the current records.  What you are doing is a separate eager-ish load 
for every 100 records, which isn't really the same thing as a eager load 
for the whole set.  For one thing, let's say in each of the 100-record 
sets, the same associated record is needed.  You end up with a separate 
associated record each eager-ish load, instead of having all of the records 
have a reference to the same object (which is what a true eager load does).

I'm glad you were able to get something that works for your use case, but 
this isn't something I plan to support in Sequel.  A slightly easier way 
may be:

# completely untested
query.eager(:association).each_slice(100) do |records| 
  query.send(:eager_load, records)
end 

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