collectiveidea/awesome_nested_set

`counter_cache` doesn't update the parent which is different class from child's counter

Closed this issue · 4 comments

We have STI classes like this:

class Parent
  acts_as_nested_set counter_cache: :counter
end

class Foo < Parent
end

class Bar < Parent
end

Then, when I try to move a Bar instance under the Foo instance (doing bar.move_to_child_of foo),
I'd like to increment counter field of the parent class Foo, but it's impossible because the increment is done by doing internally like:

self.class.increment_counter(acts_as_nested_set_options[:counter_cache], new_parent)

It virtually equals to the following in my case:

Bar.increment_counter(:counter, foo)

But they're an STI, so the SQL which will be performed is like:

UPDATE parents SET counter = COALESCE(counter, 0) + 1 WHERE parents.type = 'Bar' AND parents.id = 123

It updates no records because id=123 is a type of "Foo".
So I guess the codes in update_counter_cache should be like:

new_paraet.class.increment_counter(acts_as_nested_set_options[:counter_cache], new_parent)

If my guess is right, I'm willing to open a PullReq for it :)
Thanks.

stale commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@parndt I'm still concerning about this, please can you see it?

@parndt I'm still concerning about this, please can you see it?

@issei-m happy for you to open a pull request for this functionality with tests 😄

@parndt I've opened a pull-req addressing this issue. Could you review it? #444

Thanks!