Skip to content

.hash_tree returning NoMethodError: undefined method `[]=' for nil:NilClass #37

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
kurko opened this issue Jan 8, 2013 · 8 comments
Closed

Comments

@kurko
Copy link

kurko commented Jan 8, 2013

I have a weird problem here. I added some nodes and suddenly I can't use Taxonomy.hash_tree anymore.

I thought, "ok, given I just started out, I'll just delete everything and start over", but then what if this issue happens when I go to production?

In fact, after starting over and recreating the nodes, I haven't got the error so far.

Do you have any idea what this could be?

Thanks!

Data

Here, everything's ok.

irb(main):003:0> Company.first.taxonomies.all
  Company Load (0.6ms)  SELECT "companies".* FROM "companies" LIMIT 1
  Taxonomy Load (0.3ms)  SELECT "taxonomies".* FROM "taxonomies" WHERE "taxonomies"."store_id" = 1
=> [#<Taxonomy id: 15, name: "a", parent_id: nil, store_id: 1, created_at: "2013-01-08 01:14:34", updated_at: "2013-01-08 01:14:34">, 
#<Taxonomy id: 16, name: "aa", parent_id: 15, store_id: 1, created_at: "2013-01-08 01:14:37", updated_at: "2013-01-08 01:14:37">, 
#<Taxonomy id: 17, name: "aaa", parent_id: 16, store_id: 1, created_at: "2013-01-08 01:14:40", updated_at: "2013-01-08 01:14:40">, 
#<Taxonomy id: 18, name: "b", parent_id: nil, store_id: 1, created_at: "2013-01-08 01:14:45", updated_at: "2013-01-08 01:14:45">, 
#<Taxonomy id: 19, name: "bb", parent_id: 18, store_id: 1, created_at: "2013-01-08 01:14:48", updated_at: "2013-01-08 01:14:48">, 
#<Taxonomy id: 20, name: "aaa2", parent_id: 16, store_id: 1, created_at: "2013-01-08 01:14:59", updated_at: "2013-01-08 01:14:59">, 
#<Taxonomy id: 21, name: "aaaa", parent_id: 20, store_id: 1, created_at: "2013-01-08 01:59:28", updated_at: "2013-01-08 01:59:28">, 
#<Taxonomy id: 22, name: "aaaa2", parent_id: 20, store_id: 1, created_at: "2013-01-08 01:59:34", updated_at: "2013-01-08 01:59:34">, 
#<Taxonomy id: 23, name: "aaa3", parent_id: 20, store_id: 1, created_at: "2013-01-08 01:59:38", updated_at: "2013-01-08 01:59:38">, 
#<Taxonomy id: 24, name: "a", parent_id: 9, store_id: 1, created_at: "2013-01-08 02:34:03", updated_at: "2013-01-08 02:34:03">, 
#<Taxonomy id: 25, name: "a", parent_id: 9, store_id: 1, created_at: "2013-01-08 02:34:08", updated_at: "2013-01-08 02:34:08">]

Problematic hash_tree

Here, the error.

irb(main):006:0> Company.first.taxonomies.hash_tree
  Company Load (0.7ms)  SELECT "companies".* FROM "companies" LIMIT 1
  Taxonomy Load (0.7ms)  SELECT "taxonomies".* FROM "taxonomies" INNER JOIN (
 SELECT descendant_id, MAX(generations) as depth
 FROM "taxonomy_hierarchies"
 GROUP BY descendant_id

 ) AS generation_depth
 ON "taxonomies".id = generation_depth.descendant_id WHERE "taxonomies"."store_id" = 1 ORDER BY generation_depth.depth
NoMethodError: undefined method `[]=' for nil:NilClass
        from /Users/kurko/.rbenv/versions/1.9.3-p194-perf/lib/ruby/gems/1.9.1/bundler/gems/closure_tree-7bf33728b625/lib/closure_tree/acts_as_tree.rb:398:in `block in build_hash_tree'
        from /Users/kurko/.rbenv/versions/1.9.3-p194-perf/lib/ruby/gems/1.9.1/gems/activerecord-3.2.9/lib/active_record/relation/delegation.rb:6:in `each'
        from /Users/kurko/.rbenv/versions/1.9.3-p194-perf/lib/ruby/gems/1.9.1/gems/activerecord-3.2.9/lib/active_record/relation/delegation.rb:6:in `each'
        from /Users/kurko/.rbenv/versions/1.9.3-p194-perf/lib/ruby/gems/1.9.1/bundler/gems/closure_tree-7bf33728b625/lib/closure_tree/acts_as_tree.rb:393:in `build_hash_tree'
        from /Users/kurko/.rbenv/versions/1.9.3-p194-perf/lib/ruby/gems/1.9.1/bundler/gems/closure_tree-7bf33728b625/lib/closure_tree/acts_as_tree.rb:94:in `hash_tree'
        from /Users/kurko/.rbenv/versions/1.9.3-p194-perf/lib/ruby/gems/1.9.1/gems/activerecord-3.2.9/lib/active_record/relation/delegation.rb:14:in `block in hash_tree'
        from /Users/kurko/.rbenv/versions/1.9.3-p194-perf/lib/ruby/gems/1.9.1/gems/activerecord-3.2.9/lib/active_record/relation.rb:241:in `block in scoping'
        from /Users/kurko/.rbenv/versions/1.9.3-p194-perf/lib/ruby/gems/1.9.1/gems/activerecord-3.2.9/lib/active_record/scoping.rb:98:in `with_scope'
        from /Users/kurko/.rbenv/versions/1.9.3-p194-perf/lib/ruby/gems/1.9.1/gems/activerecord-3.2.9/lib/active_record/relation.rb:241:in `scoping'
        from /Users/kurko/.rbenv/versions/1.9.3-p194-perf/lib/ruby/gems/1.9.1/gems/activerecord-3.2.9/lib/active_record/relation/delegation.rb:14:in `hash_tree'
        from /Users/kurko/.rbenv/versions/1.9.3-p194-perf/lib/ruby/gems/1.9.1/gems/activerecord-3.2.9/lib/active_record/associations/collection_proxy.rb:100:in `method_missing'
        from (irb):6
        from /Users/kurko/.rbenv/versions/1.9.3-p194-perf/lib/ruby/gems/1.9.1/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start'
        from /Users/kurko/.rbenv/versions/1.9.3-p194-perf/lib/ruby/gems/1.9.1/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start'
        from /Users/kurko/.rbenv/versions/1.9.3-p194-perf/lib/ruby/gems/1.9.1/gems/zeus-0.12.0/lib/zeus/rails.rb:117:in `console'
        from /Users/kurko/.rbenv/versions/1.9.3-p194-perf/lib/ruby/gems/1.9.1/gems/zeus-0.12.0/lib/zeus.rb:105:in `block in command'
... 8 levels...
        from /Users/kurko/.rbenv/versions/1.9.3-p194-perf/lib/ruby/gems/1.9.1/gems/zeus-0.12.0/lib/zeus.rb:61:in `go'
        from /Users/kurko/.rbenv/versions/1.9.3-p194-perf/lib/ruby/gems/1.9.1/gems/zeus-0.12.0/lib/zeus.rb:67:in `block (3 levels) in go'
        from /Users/kurko/.rbenv/versions/1.9.3-p194-perf/lib/ruby/gems/1.9.1/gems/zeus-0.12.0/lib/zeus.rb:67:in `fork'
        from /Users/kurko/.rbenv/versions/1.9.3-p194-perf/lib/ruby/gems/1.9.1/gems/zeus-0.12.0/lib/zeus.rb:67:in `block (2 levels) in go'
        from /Users/kurko/.rbenv/versions/1.9.3-p194-perf/lib/ruby/gems/1.9.1/gems/zeus-0.12.0/lib/zeus.rb:63:in `each'
        from /Users/kurko/.rbenv/versions/1.9.3-p194-perf/lib/ruby/gems/1.9.1/gems/zeus-0.12.0/lib/zeus.rb:63:in `block in go'
        from /Users/kurko/.rbenv/versions/1.9.3-p194-perf/lib/ruby/gems/1.9.1/gems/zeus-0.12.0/lib/zeus.rb:61:in `loop'
        from /Users/kurko/.rbenv/versions/1.9.3-p194-perf/lib/ruby/gems/1.9.1/gems/zeus-0.12.0/lib/zeus.rb:61:in `go'
        from /Users/kurko/.rbenv/versions/1.9.3-p194-perf/lib/ruby/gems/1.9.1/gems/zeus-0.12.0/lib/zeus.rb:67:in `block (3 levels) in go'
        from /Users/kurko/.rbenv/versions/1.9.3-p194-perf/lib/ruby/gems/1.9.1/gems/zeus-0.12.0/lib/zeus.rb:67:in `fork'
        from /Users/kurko/.rbenv/versions/1.9.3-p194-perf/lib/ruby/gems/1.9.1/gems/zeus-0.12.0/lib/zeus.rb:67:in `block (2 levels) in go'
        from /Users/kurko/.rbenv/versions/1.9.3-p194-perf/lib/ruby/gems/1.9.1/gems/zeus-0.12.0/lib/zeus.rb:63:in `each'
        from /Users/kurko/.rbenv/versions/1.9.3-p194-perf/lib/ruby/gems/1.9.1/gems/zeus-0.12.0/lib/zeus.rb:63:in `block in go'
        from /Users/kurko/.rbenv/versions/1.9.3-p194-perf/lib/ruby/gems/1.9.1/gems/zeus-0.12.0/lib/zeus.rb:61:in `loop'
        from /Users/kurko/.rbenv/versions/1.9.3-p194-perf/lib/ruby/gems/1.9.1/gems/zeus-0.12.0/lib/zeus.rb:61:in `go'
        from -e:1:in `<main>'
@mceachen
Copy link
Collaborator

mceachen commented Jan 8, 2013

Do you have a db backup from when it was broken? If you can share the result of:

SELECT id, parent_id, name
FROM taxonomies

We can shove the data into a spec and try to reproduce the issue.

@mceachen
Copy link
Collaborator

ping

@kurko
Copy link
Author

kurko commented Jan 14, 2013

Hey @mceachen, pong

Sorry, it has been a busy week. I destroyed all records and started from scratch. I got no errors so far.

I will try reproducing the issue again in the next couple of days.

Thanks.

@mceachen
Copy link
Collaborator

OK, then, I don't think we can move forward here. Feel free to reopen this issue or open a new one if you find this happen again. With a data dump we'll be able to quickly get the to bottom of whatever happened.

If you've got a multithreaded app that's creating lots of hierarchies, I'm working on integrating monogamy with closure_tree to make sure the transaction boundaries are correct. You may have been bitten by duplicative rows, but I can't even guess without your data when it was broken.

@mceachen
Copy link
Collaborator

mceachen commented Sep 7, 2015

I looked at this more in the light of #146, and the issue here is that you are incanting hash_tree against a relation object from a non-closure-tree model. ActiveRecord added .hash_tree for any collection of your tree model, but .hash_tree can't span multiple roots.

If you don't mind multiple selects:

Company.first.taxonomies.map(&:hash_tree).reduce(&:merge)

@kurko
Copy link
Author

kurko commented Sep 11, 2015

👍 2 years later, but still relevant 😊

@mceachen
Copy link
Collaborator

did you try the .map.reduce?

@dhurba87
Copy link

@mceachen
.map.reduce works but the query is too much slow. :(
Updating version > 6.0 worked in my case.

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

No branches or pull requests

3 participants