Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eager loading and preload! method #1

Open
bobmaerten opened this issue Oct 3, 2017 · 2 comments
Open

Eager loading and preload! method #1

bobmaerten opened this issue Oct 3, 2017 · 2 comments

Comments

@bobmaerten
Copy link

Hi!

First of all thank you for providing a fresh approach to exporting data. It's just the kind of thing I was looking for. Though very young, this gem has found a place in our controllers and rake tasks dedicated to output data from our DB.

Still... youngness, while sometimes good for freshness, is also sometimes a pain for documentation and comprehension of intents. I had trouble sorting out the necessity of overriding the object_class method, to let specifying whatever-we-wanted-for-export-class-name.

And now I have another bad time with eager-loading. I want to export customers data with a lot of nested classes, so I naively pass a eager-loaded scope to my exporter:

scope = Customer.where(id: customer_ids).includes(:addresses, :orders => :order_items)
MyCustomerExport.new(scope).to_xlsx.read

but it does not seems to works by looking at my dev logs.

I also noticed an empty preload! method in the export.rb but I don't really know what to do more for the objects to use eager loading when exporter parses the objects. Any tips on that?

Sorry for not being very clear (not english native). I tried to figure out how Xport::Export class works, but it seems to be much more meta than I can't take! ;)

@ebeigarts
Copy link
Member

Hi, sorry for the long delay, this project is still young and was extracted from one of our private projects, so there are some undocumented/internal features, like preload!, that could be used like:

class MyCustomerExport < Xport::Export
  def preload!
    @objects = @objects.includes(:addresses, :orders => :order_items)
  end
end

Anyway, your example looks ok and it should have preloaded the objects.
Do you receive an error or the preloading doesn't work?

@bobmaerten
Copy link
Author

I'm not quite sure how to describe this but even before passing scop, even within preload! method, my "included" scope requests seems to be un-optimised.

When I call sub-elements inside block (or defined method), eager loading seems to be bypassed. I looked again in the gem's code, that should not be occurring as there's a object.each_with_index involved that should keep eager loading for each elements.

Maybe it's a too complex one (lots of nested elements needed for export calculations). I'll try in another simpler export.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants