acts_as_tree icon indicating copy to clipboard operation
acts_as_tree copied to clipboard

Bug with `leaf?` method

Open marcinwierzbicki opened this issue 7 years ago • 4 comments

leaf? method given me exception:

TypeError: nil can't be coerced into Fixnum from /usr/local/bundle/gems/activerecord-5.0.0.1/lib/active_record/associations/collection_association.rb:319:in `+'

I have the same error with this: model.association.children.size.zero?

Can we change implementation of leaf? https://github.com/amerine/acts_as_tree/blob/4fa898054e7b01962ec4b65919c844ee95554fa2/lib/acts_as_tree.rb#L352 method from:

def leaf?
  children.size.zero?
end

to:

def leaf?
  children.blank?
end

?

Video: https://youtu.be/hutkAa39JcU

marcinwierzbicki avatar Sep 08 '17 13:09 marcinwierzbicki

This is strange, because children should be a has_many association, which cannot return nil.

Maybe you have a method on your model that overrides the default children association?

felixbuenemann avatar Sep 08 '17 18:09 felixbuenemann

I have not method that overrides this chilldren association. My first idea was eager loading in ActiveRecord - my video present this behavior.

marcinwierzbicki avatar Sep 08 '17 18:09 marcinwierzbicki

This sounds similar to the ActiveRecord bug described in issue rails/rails#28579.

Could you try if the bug is fixed by using rails master?

felixbuenemann avatar Sep 08 '17 18:09 felixbuenemann

Bug is fixed in rails#master

marcinwierzbicki avatar Sep 15 '17 08:09 marcinwierzbicki