Register STI descendants for collection updates#204
Conversation
leastbad
left a comment
There was a problem hiding this comment.
So, I still appreciate this solution, as off-beat as it is and I'm basically good to go with it. I would ask that you append a tiny example of what a has_many descendants: [Foo] would look like, just for documentation posterity.
However, I do have a thought: what if we actually didn't throw out the descendants array and actually used it as a list on line 93? This would allow the developer to specify a list of classes that might exclude some.
The other thing that just popped into my head: is there such a thing as dynamically created STI classes? Not in typical operation, but I'm trying to think if there's a pattern which a popular gem might use that arbitrarily creates model classes.
Given autoloading in production, maybe that's not an issue, either. Just trying to black-hat this fix with the weirdest fuzzing ideas I can muster.
I actually thought the same. Just for consistency, would we want to go with a string/symbol specification instead, like class_name? |
Hmm nothing that immediately springs to mind... every STI model has to write its type into the type column so... if we're talking about ActiveRecord, that would be quite backwards? |
I think that your proposed syntax is already perfect. I was just hinting that it could be fed into the list as a splat, or such. |
|
I feel like If Fussy, perhaps. But consistency is underrated. |
very possible, but I was actually thinking it'd be more consistent with https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-has_many
|
|
I thought we still have a need to get the constantized version into the call so that it's in object space when the method runs? or is that now moot because we're not using I know that symbols are strings, ish, but in our docs (and tests) if we're emulating I have a perception that the standard Rails symbol idiom is to use downcase. Is that actually true? |
ah now I got you, yeah I actually got that mixed up.
In the context of |
Problem
I noticed that when working with STI models in a
has_manyrelation, the collection callbacks would only trigger updates for the base class, not its descendants. So in short:would only ever trigger updates if I added an instance of
Blockto@section.blocks, not an instance ofComment.Remedy
Registering collections for every descendant of a
reflection.klassresolved this: https://github.com/stimulusreflex/cable_ready/compare/updatable-sti?expand=1#diff-90be2c3ebba84a166909a4f0528f7698ecea6f124f3d96c19a703e02320c38f9R93However in the
developmentandtestenvironments,Block.descendantswould return an empty array because of lazy loading. Ahackcompromise is to add an optiondescendants:to ourhas_manycall:https://github.com/stimulusreflex/cable_ready/compare/updatable-sti?expand=1#diff-90be2c3ebba84a166909a4f0528f7698ecea6f124f3d96c19a703e02320c38f9R93
https://github.com/stimulusreflex/cable_ready/compare/updatable-sti?expand=1#diff-90be2c3ebba84a166909a4f0528f7698ecea6f124f3d96c19a703e02320c38f9R44
This actually doesn't do anything else than putting the class constant into ObjectSpace. I.e. it doesn't matter where you call
Comment, it just has to be before the collections are registered. Still, it feels like the most concise and documentable solution 💅EDIT: now explicitly
constantizingdescendants from an array of symbols or strings: