-
Notifications
You must be signed in to change notification settings - Fork 463
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
update_descendants_with_new_ancestry in after_update #589
update_descendants_with_new_ancestry in after_update #589
Conversation
There's a new |
@kbrock have you had a chance to look into this? |
@kshnurov thanks for the ping. When I modify values in a model like a field ( If you move the code into a Also, is it possible that def update_slug
self.slug = [parent.slug, self.name].join('/')
end Though, having the I need to document a good way to build a field that is composed of the ancestry |
Hi @kbrock
Me too, this is just an example, doesn't matter which callback you use, I use
This is just a mockup example, a typical use would be some kind of title or name, but once again - it's not the issue here. |
Hello @kshnurov Thank you for the clarification. it sounds like we are on the same page. There is definitely an issue when there are multiple version of I'll take another look at this. This does look like it is just a conversion from one hook to another.
For this case, introducing a database only update tends to works better, but that will not update in memory nodes, so that has the potential to introduce edge cases. /ASIDE: Wonder if this pattern is common enough to warrant introduction into the gem itself. |
Yes, since we should update descendants after the record is saved, not before. Also, there's a reason why I used
Database only update obviously won't work with this case or any other callbacks, like updating data in your search DB, and you'll end up with inconsistent data. I think they're evil and shouldn't exist, because they will break pretty much everything, including a simple cache (since
Using parent values is inevitable if you have friendly urls like |
aa17802
to
00647bc
Compare
@kbrock as you can see tests fail with PG, since DB-only updates don't trigger callbacks. Skipped that test for sql strategy. |
@kbrock is there anything holding you from merging this? |
@kshnurov I appreciate where you are coming from. I'm concerned that after update will need to save the child records twice instead of once. Is your thought that we will very rarely update the ancestry, so having that operation a double save is not bad? Or I guess it is possible that the primary record will be changed, maybe double saved, then the followup records will only get only updated once? |
@kbrock why will it save twice? It won't, child records will be updated only once - when parent If someone adds their own callback to update children on parent change, it's up to them to avoid running callbacks twice in such rare case. Anyway there won't be any double-saving to DB since all changes will happen on the first run, second run won't change the record. |
@kbrock 1,5 months and this simple PR didn't move. Should we consider this gem unmaintained? |
@kbrock ? |
Sorry for the delay. Updating the callback order is potentially a breaking change for the hundred(s?) of projects that depend upon this gem. I am looking into fixing this for sql based updates |
* Fix: descendants ancestry is now updated in after_update callbacks stefankroes#589 * Document updated grammar stefankroes#594 * Documented `update_strategy` stefankroes#588 * Fix: fixed has_parent? when non-default primary id stefankroes#585 * Documented column collation and testing stefankroes#601 stefankroes#607 * Added initializer with default_ancestry_format stefankroes#612 * ruby 3.2 support stefankroes#596
* Fix: materialized_path2 strategy stefankroes#597 * Fix: descendants ancestry is now updated in after_update callbacks stefankroes#589 * Document updated grammar stefankroes#594 * Documented `update_strategy` stefankroes#588 * Fix: fixed has_parent? when non-default primary id stefankroes#585 * Documented column collation and testing stefankroes#601 stefankroes#607 * Added initializer with default_ancestry_format stefankroes#612 * ruby 3.2 support stefankroes#596
* Fix: materialized_path2 strategy stefankroes#597 * Fix: descendants ancestry is now updated in after_update callbacks stefankroes#589 * Document updated grammar stefankroes#594 * Documented `update_strategy` stefankroes#588 * Fix: fixed has_parent? when non-default primary id stefankroes#585 * Documented column collation and testing stefankroes#601 stefankroes#607 * Added initializer with default_ancestry_format stefankroes#612 * ruby 3.2 support stefankroes#596
- this is called after a save (/via stefankroes#589 ), dropping `new_record?` - putting `ancestry_changed?` in the callback definition. not needed in the method - had wanted to remove `sane_ancestor_ids?` since validations already ran Unfortunately, `update_attribute` (read: no validations) allows bogus ancestry through. Decided to continue skipping updates if the ancestry is deemed not valid. It can only get this way if a user skips validations. Not sure the desired behavior if something is corrupting ancestor's ancestry value. I can see reason why we may want to update them all. For now, keeping this skip updates check in place
this was removed by 0fcd12f stefankroes#589 adding fields back. added tests to exercise
this was removed by 0fcd12f stefankroes#589 adding fields back. added tests to exercise
this was removed by 0fcd12f stefankroes#589 adding fields back. added tests to exercise
this was removed by 0fcd12f stefankroes#589 adding fields back. added tests to exercise
this was removed by 0fcd12f stefankroes#589 adding fields back. added tests to exercise
this was removed by 0fcd12f stefankroes#589 adding fields back.
this was removed by 0fcd12f stefankroes#589 adding fields back. added tests to exercise
this was removed by 0fcd12f stefankroes#589 adding fields back. added tests to exercise
this was removed by 0fcd12f stefankroes#589 adding fields back. added tests to exercise
this was removed by 0fcd12f stefankroes#589 adding fields back. added tests to exercise
this was removed by 0fcd12f stefankroes#589 adding fields back. added tests to exercise
This was removed by stefankroes#589 Adding it back.
This was removed by stefankroes#589 Adding it back.
this was removed by 0fcd12f stefankroes#589 adding fields back. added tests to exercise
This was removed by stefankroes#589 Adding it back.
this was removed by 0fcd12f stefankroes#589 adding fields back. added tests to exercise
added a few changes from master's CHANGELOG Highlighted that the ancestry_primary_key_format had changed (removed in stefankroes#589 )
This simple code will not work as expected when you update a parent's slug - descendants won't have the slug changed.
That is because
update_descendants_with_new_ancestry
is run withbefore_save
and theparent
method loads an unchanged version from the database.This PR moves
update_descendants_with_new_ancestry
toafter_update
.There's no tests for such case right now, but I've tested it with my code and it works. Testing with
update_strategy = :sql
would be appreciated, though the change is pretty simple and should just work.