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
Nested eager loading on polymorphic association #8005
Comments
|
I just ran into this today and it is kinda frustrating. There is probably a downside to just removing the error entirely but there should be some mechanism for handling this case in my opinion. |
|
I agree with you @thegrubbsian. That's why I didn't made it a pull request. Ideally, when the Preloader know that the record came from a STI/Polymorphic association, it should assume that therefore nested missing relationships should be ignore since they are not predictable. This look like bit of a changes since at the moment the Preloader is not aware of where the query/parent object. Meanwhile, I made a small gem that monkey patch the preloader within a block. I hope you find it useful. |
|
Hey, thanks @unixcharles, I just ran into this issue and your gem seems to be what I need. |
|
Hey @desireco, I'm glad it was helpful for you. |
|
Anyone at rails core: should this turn into a pull request against rails? |
|
I'm not quite sure about this one. It would be good to hear what @jonleighton thinks. |
|
I think this could be a reasonable feature, but my enthusiasm will be affected by the complexity of the implementation. If someone wants to take a stab at a patch, go ahead. |
|
any progress here? :( |
|
Me too. |
|
+1 - I would really like this to be included in rails as well. For now, monkey-patching it will work for my purposes... EDIT: Looks like the gem is no longer working with rails 3.2.13. Would love to hear of any other solutions to this. Thanks! |
|
+1 |
1 similar comment
|
+1 |
|
I agree with @jonleighton. If someone want to open a pull request and we will discuss the implementation. I'm closing since this one is a feature request. |
This allows for nested eager loading of polymorphic associations Resolves rails#8005
|
Hi everyone, I submitted a PR for this: #17479. Give it a test! |
|
@seanlinsley I love it, does exactly what I need. This has been on my "open issues" list for such a long time. Thanks so much for dealing with it! |
This allows for nested eager loading of polymorphic associations Resolves rails#8005
The current implementation of the preloader prevent you from eager loading inexistent association.
preloader.rb#L144
Which seem reasonable but then it as the side effect of preventing nested eager loading on certain type of polymorphic association. Keep reading!
Here an exemple involving space exploration and experimentation on animals.
As you can see, ActiveRecord seem concern that you try to retrieve the army rank of an animal.
This is sad because as a side effect, it prevent you from eager loading the legit rank association of humanoid astronaut.
Given that polymorphic association can point to any model, records that don't have the association could be simply ignored since there is no warranty that the association is not valid for the other records.
I suppose this also apply to STI.
I would suggest be to simply remove that check, filter out records with missing association like this and leave it up to the programmer to not eager load fantasy associations.
Please advice, thanks for your time!😄
The text was updated successfully, but these errors were encountered: