You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
After installing the closure_tree gem (with bundler) and adding the acts_as_tree line to my model (and performing the associated migrations), I get the following error when attempting to create a node. (The model I'm attempting to use closure_tree with in this case is called Item):
> Item.create merchant_item_id: 1, name: "lol"
NoMethodError: undefined method `find_each' for []:Array
Did you mean? find_all
from /Users/antondewinter/.rvm/gems/ruby-2.4.2/gems/closure_tree-6.6.0/lib/closure_tree/hierarchy_maintenance.rb:85:in `block in rebuild!'
merchant_item_id is a required field in the model (and I think is unrelated to this error). When I first tried to create a node, the item_hierarchies table was empty.
I tried to trace this error to a recent change in the closure_tree codebase, and I think it is related to this commit. Which sort of makes sense: when there are no children, an empty array is returned (instead of an AR association), meaning one cannot use #find_each; which would cause this error. When I modified Line 85 back to just using regular .each, there were no errors and I was able to create nodes.
Is this a bug or am I doing something wrong?
Thanks!
The text was updated successfully, but these errors were encountered:
It might be safer to apply either find_each or each, based on which method exists on the children collection--i.e., the collection is a static array, then call each, if the collection is active_record magicks, call find_each.
If you think that sounds sane, I'm more than happy to take a PR (especially if it has a breaking test!)
I made a mistake. It turns out there was code in my codebase that overrode the #children method, causing these issues. Once I removed that bit of code, there were none of the errors I described above. Sorry for leading you on a goose chase!
Hi,
After installing the
closure_tree
gem (with bundler) and adding theacts_as_tree
line to my model (and performing the associated migrations), I get the following error when attempting to create a node. (The model I'm attempting to useclosure_tree
with in this case is calledItem
):merchant_item_id
is a required field in the model (and I think is unrelated to this error). When I first tried to create a node, theitem_hierarchies
table was empty.I tried to trace this error to a recent change in the
closure_tree
codebase, and I think it is related to this commit. Which sort of makes sense: when there are no children, an empty array is returned (instead of an AR association), meaning one cannot use#find_each
; which would cause this error. When I modified Line 85 back to just using regular.each
, there were no errors and I was able to create nodes.Is this a bug or am I doing something wrong?
Thanks!
The text was updated successfully, but these errors were encountered: