On Tuesday, June 21, 2016 at 8:12:31 AM UTC-7, Tiago Cardoso wrote: > > This might be a design decision, and I'd like to understand it before > perceiving it as a bug. Here is my script: > > require 'sequel' > require 'logger' > > DB = Sequel.sqlite > DB.loggers = [Logger.new(STDOUT)] > Sequel::Model.db.extension(:pagination) > > DB.create_table? :cars do > primary_key :id > end > DB.create_table? :wheels do > foreign_key :car_id, :cars > column :number, Integer > end > > class Car < Sequel::Model > one_to_many :wheels > end > class Wheel < Sequel::Model > many_to_one :car > end > > puts "database is set..." > > 3.times do > car = Car.create > car.add_wheel number: 1 > car.add_wheel number: 2 > car.add_wheel number: 3 > car.add_wheel number: 4 > end > > puts "eaching:" > Car.eager(:wheels).map(&:wheels) > puts "with .all: " > Car.eager(:wheels).all.map(&:wheels) > puts "with .pagination: " > Car.eager(:wheels).paginate(1, 4).all.map(&:wheels) > > DB.drop_table :wheels, :cars > > > if you look at the db logs, you see that associations are only eager load > by explicit call to .all . I don't understand why, because if I explicit > pass to a dataset that it has to eager load an association to its models, I > expect them to be eager loaded as soon as I start iterating on them. If I > don't call .all, it effectively breaks the eager chainability. >
Only eager loading if #all is called is the expected and documented behavior. You cannot eager load without having all rows up front. If you really want to eager load without having to call #all, you can use the eager_each plugin, which makes #each call #all for eagerly loaded datasets. > You might have good reasons for not introducing it, but think about this > use case: I want to paginate the collection. I usually render the list of > elements in html and add the pagination buttons in the end. If I keep the > paginated collection in a variable, I have access to the pagination > extension methods, like total_pages, and kaminari/will_paginate work out of > the box, but iterating on the collection breaks eager call. If I paginate > then call all, I have the collection page eager loaded into an array with > the association eager loaded, but the object doesn't contain the pagination > extensions, thereby crashing will_paginate/kaminari integration. > > My workaround is to keep 2 instances in memory (the eager loaded and the > pagination plugin extended), which while working, doesn't seem very > convenient. > The eager_each plugin may work for such cases. I haven't used will_paginate/kaminari and unfortunately can't provide any specific guidance for them. With Sequel's pagination extension, you would just call #all on the dataset returned by #paginate. 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 https://groups.google.com/group/sequel-talk. For more options, visit https://groups.google.com/d/optout.
