Rails - Acts as nested - Enforcing a Max Level -


i'm using gem 'nested_set' comment threading.

what want prevent comment level going more 2 levels deep. tired doing this:

class comment < activerecord::base     ....     before_save :ensure_max_nestedset_level   private      # don't want comments go more 2 levels deep. that's overkill     def ensure_max_nestedset_level       if self.level > 2         self.level = 2       end     end  end 

but looks cant set level obtain objects level. goal being enforce max of 2 levels deep comment threading. can suggest way enforce happening?

the use case being:

comment main (level 0)    comment reply (level 1)      comment reply xxxx (level 2) 

when user replies last 1 (about xxxx) don't want comment set level of 3, want cap @ 2.

ideas? thanks

this seems work, though there might better solution.

class comment < activerecord::base   acts_as_nested_set    after_save :check_level    def check_level     if level > 2       move_to_child_of(parent.parent)     end   end end 

note changing before_save makes fail, don't know why. perhaps has rebalancing of tree?


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -