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 :self_and_ancestors breaks :ancestry_path #372

Open
dup2 opened this issue Jan 4, 2021 · 1 comment
Open

Eager loading :self_and_ancestors breaks :ancestry_path #372

dup2 opened this issue Jan 4, 2021 · 1 comment

Comments

@dup2
Copy link

dup2 commented Jan 4, 2021

For an AR model VirtualPath used for a DB based file system, we use this excellent gem.

When loading a bunch of the models after applying access control and other restrictions, we build up the hierarchy for a recursive "ls" style output and use ancestry_path for the parts.

To speed up loading the ancestors, we try to use eager loading:
However, this mixes up the order of the ancestry path elements.

Example queries and output:

irb(main):063:0> VirtualPath.limit(4).map(&:ancestry_path)
=> [["Root"], ["Root", "FunnyFolder-1"], ["Root", "FunnyFolder-2"], ["Root", "FunnyFolder-2", "Funny-Sub-2"]]
irb(main):064:0> VirtualPath.limit(4).eager_load(:self_and_ancestors).map(&:ancestry_path)
=> [["Root"], ["FunnyFolder-1", "Root"], ["FunnyFolder-2", "Root"], ["Funny-Sub-2", "FunnyFolder-2", "Root"]]

Any solutions to this? Other users seem to cache the ancestry_path for speeding up this use case..
Can't this be done with AR queries only?

@dup2
Copy link
Author

dup2 commented Jan 5, 2021

It seems that the ordering is lost when doing eager_load and the method ancestory_path relies on this order.

Using .eager_load(:self_and_ancestors).order("virtual_path_hierarchies.generations" => :asc) seems to fix this.

Any clue why this is the case?

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

1 participant