Skip to content

undefined method `find_each' for []:Array #286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
adewinter opened this issue Oct 5, 2017 · 3 comments
Closed

undefined method `find_each' for []:Array #286

adewinter opened this issue Oct 5, 2017 · 3 comments

Comments

@adewinter
Copy link

Hi,

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!

@mceachen
Copy link
Collaborator

mceachen commented Oct 7, 2017

Nice sleuthing!

I was going to suggest that it's a rails version issue with find_each, but that seems to be fairly stable since v3.0. Here's the PR associated with that diff.

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!)

@adewinter
Copy link
Author

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!

I'm going to go ahead and close this ticket.

@nbekirov
Copy link

nbekirov commented Oct 9, 2017

I was just looking into this and was about to post that it's not reproducible on Rails 5.1.4... 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants