Improve performance of self_and_descendants #122

Merged
merged 2 commits into from Mar 16, 2012

2 participants

@marktorrance

This modified version of self_and_descendants is semantically equivalent and passes all tests, but it uses only the lft field of the records it queries, which means it can benefit from an index on that field if one exists on your table.

@parndt parndt and 1 other commented on an outdated diff Mar 16, 2012
lib/awesome_nested_set/awesome_nested_set.rb
@@ -336,7 +336,8 @@ def level
# Returns a set of itself and all of its nested children
def self_and_descendants
nested_set_scope.where([
- "#{self.class.quoted_table_name}.#{quoted_left_column_name} >= ? AND #{self.class.quoted_table_name}.#{quoted_right_column_name} <= ?", left, right
+ "#{self.class.quoted_table_name}.#{quoted_left_column_name} >= ? AND #{self.class.quoted_table_name}.#{quoted_left_column_name} <= ?", left, right
@parndt
parndt added a note Mar 16, 2012

Shouldn't it be < not <= on the second half?

I guess it doesn't matter, since lft is always less than rgt for any given node. But I can change that if you like.

@parndt
parndt added a note Mar 16, 2012

I was just thinking that it would be a more correct version?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@parndt parndt merged commit 18fe58a into collectiveidea:master Mar 16, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment