Correctly set the ancestry column when creating a child within an after_* callback #57

Closed
wants to merge 2 commits into
from

Conversation

Projects
None yet
4 participants

jstirk commented Jun 10, 2011

I noticed an issue where creating a child record with the ancestry field set to just the parent id, rather than the entire parent path.

I have added a test reproducing the behaviour, and a fix to the code.

The main issue looked to be that the child_ancestry method was using the ancestry path before the save (using _was) which for new records was obviously blank.

This revision uses the current value of the parent's ancestry path, and adds a child_ancestry_was method to return the old behaviour (with a more descriptive name).

The change to update_descendants_with_new_ancestry was due to it relying on the old behaviour. The revised version explicitly replaces the old parent ancestry snippet (using child_ancestry_was) with the new path.

Please let me know if you need any additional information, or if you would like me to revise the patch.

Jason Stirk added some commits Feb 24, 2011

Failing test: create child in after_create callback
Creating a child in a after_* callback results in the child's ancestry
column containing only the parent id, and not the entire parent ancestry
path.
Fix creating children in after_* callbacks
Previously creating children in an after_* callback would cause the
child's ancestry column to contain just the parent id and not the entire
ancestry path.

A workaround was to call self.reload in the after_* callback, but this
commit makes the process work automatically.

The main issue looked to be that the child_ancestry method was using the
ancestry path before the save (using _was) which for new records was
obviously blank.

This revision uses the current value of the parent's ancestry, and adds
a child_ancestry_was method to return the old behaviour (with a more
descriptive name).

The change to update_descendants_with_new_ancestry was due to it relying
on the old behaviour. The revised version explicitly replaces the old
parent ancestry snippet (using child_ancestry_was) with the new one
path.

I just burned 12 hours on the same problem. Why was this pull request not committed?

Owner

stefankroes commented Mar 20, 2014

This pull request is three years old. I don't know why it was never merged. If you were to update it so it can be merged, I'll take another look at it.

Collaborator

kbrock commented Feb 8, 2016

please see #263

@kbrock kbrock closed this Feb 8, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment