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

Cannot "includes" other records when using this #25

Closed
QuotableWater7 opened this issue Jun 17, 2016 · 2 comments · Fixed by #34
Closed

Cannot "includes" other records when using this #25

QuotableWater7 opened this issue Jun 17, 2016 · 2 comments · Fixed by #34

Comments

@QuotableWater7
Copy link

I'm not sure how difficult it would be to do, but I realized Model.includes(:something).each_instance will return back records that do not have the associations preloaded.

@afair
Copy link
Owner

afair commented Sep 1, 2016

This may be difficult, but it certainly would be nice to do. This library mostly performs the row fetching and returns each row as either a Hash or Model.instantiate(). It does this outside of ActiveRecord internals that work to map the database results back into Objects (like what an ORM does). I think it would be easier to introduce Cursors into ActiveRecord than do this :-)

I'll leave this open in case some aspiring coder can put together a Pull Request, or if I have any ideas in the future.

@lvonk
Copy link

lvonk commented Oct 31, 2016

Hi,

I created a simpler cursor (less features that is...) inspired by this cursor that supports the includes and works for our use case.

I found that ActiveRecord used the ActiveRecord::Associations::Preloader to make includes work. See the exec_queries method in ActiveRecord::Relation. The preload takes an array of records and an array of assocications. So in order to make it work the fetch method will to return an array instead of a single row. When doing so each time a block in fetched it will do the preloading of the associations.

This cursor is then used as follows:

Cursor.new(Model.includes(foos: [:bar], :bar_foos).where(id < 10000000).order(:id)).find_each do |model|
  # do something
end

The entire cursor code is here: https://gist.github.com/lvonk/9fefe788616a22170bf53fadd7bcb66a

Will this work in this gem you think? If so I could try to create a PR for this...

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

Successfully merging a pull request may close this issue.

3 participants