activegraph icon indicating copy to clipboard operation
activegraph copied to clipboard

has_many relationship throwing a NameError when rel_class option is an array of symbols

Open anshizzle opened this issue 5 years ago • 2 comments

As explained above, I'm getting the following NameError when calling a has_many relationship

from /usr/local/bundle/gems/activesupport-5.1.4/lib/active_support/inflector/methods.rb:269:in `const_get'

Additional information which could be helpful if relevant to your issue:

Code example (inline, gist, or repo)

I have a class with the following has_many relationships:

  has_many :out, :classes, rel_class: :StudentOf, unique: true, model_class: :Class
  has_many :out, :extracurriculars, rel_class: :MemberOf, unique: true, model_class: :Extracurricular

and am adding the below:

has_many :out, :activities, rel_class: [:StudentOf, :MemberOf], unique: true

The documentation indicates that an array should be valid input for the rel_class, so I'm wondering if this is a Neo4j bug or if I'm not declaring the option correctly.

Has anyone run into this before? I'm currently working around it by writing a Cypher query to pull the relationship.

Here is the entire backtrace:

/usr/local/bundle/gems/activesupport-5.1.4/lib/active_support/inflector/methods.rb:269:in `const_get'
/usr/local/bundle/gems/activesupport-5.1.4/lib/active_support/inflector/methods.rb:269:in `block in constantize'
/usr/local/bundle/gems/activesupport-5.1.4/lib/active_support/inflector/methods.rb:267:in `each'
/usr/local/bundle/gems/activesupport-5.1.4/lib/active_support/inflector/methods.rb:267:in `inject'
/usr/local/bundle/gems/activesupport-5.1.4/lib/active_support/inflector/methods.rb:267:in `constantize'
/usr/local/bundle/gems/activesupport-5.1.4/lib/active_support/core_ext/string/inflections.rb:66:in `constantize'
/usr/local/bundle/gems/neo4j-9.2.4/lib/neo4j/active_node/has_n/association.rb:128:in `relationship_class'
/usr/local/bundle/gems/neo4j-9.2.4/lib/neo4j/active_node/has_n/association.rb:26:in `derive_model_class'
/usr/local/bundle/gems/neo4j-9.2.4/lib/neo4j/active_node/has_n/association.rb:61:in `target_class_names'
/usr/local/bundle/gems/neo4j-9.2.4/lib/neo4j/active_node/has_n/association.rb:79:in `target_classes_or_nil'
/usr/local/bundle/gems/neo4j-9.2.4/lib/neo4j/active_node/has_n.rb:536:in `association_target_class'
/usr/local/bundle/gems/neo4j-9.2.4/lib/neo4j/active_node/has_n.rb:518:in `association_query_proxy'
/usr/local/bundle/gems/neo4j-9.2.4/lib/neo4j/active_node/has_n.rb:205:in `association_query_proxy'
/usr/local/bundle/gems/neo4j-9.2.4/lib/neo4j/active_node/has_n.rb:235:in `fresh_association_proxy'
/usr/local/bundle/gems/neo4j-9.2.4/lib/neo4j/active_node/has_n.rb:219:in `block (2 levels) in association_proxy'
/usr/local/bundle/gems/neo4j-9.2.4/lib/neo4j/active_node/has_n.rb:218:in `each'
/usr/local/bundle/gems/neo4j-9.2.4/lib/neo4j/active_node/has_n.rb:218:in `inject'
/usr/local/bundle/gems/neo4j-9.2.4/lib/neo4j/active_node/has_n.rb:218:in `block in association_proxy'
/usr/local/bundle/gems/neo4j-9.2.4/lib/neo4j/active_node/has_n.rb:199:in `block in association_proxy_cache_fetch'
/usr/local/bundle/gems/neo4j-9.2.4/lib/neo4j/active_node/has_n.rb:198:in `fetch'
/usr/local/bundle/gems/neo4j-9.2.4/lib/neo4j/active_node/has_n.rb:198:in `association_proxy_cache_fetch'
/usr/local/bundle/gems/neo4j-9.2.4/lib/neo4j/active_node/has_n.rb:215:in `association_proxy'
/usr/local/bundle/gems/neo4j-9.2.4/lib/neo4j/active_node/has_n.rb:399:in `block in define_has_many_methods'

Runtime information:

Neo4j database version: 3.3.4 neo4j gem version: 9.2.4 neo4j-core gem version: 8.1.4

anshizzle avatar Oct 09 '18 19:10 anshizzle

The documentation indicates that an array should be valid input for the rel_class

I assume you're referring to the comments in the source code? I had never noticed reference to this before myself. The documentation is incorrect. rel_class does not accept an array. Maybe it did at one point in time, but it does not currently. There are multiple places in the Association definition where rel_class is assumed to not be an array, I see no support for handling an array, and there are no tests for an array input.

The method validating the rel_class option should be updated to throw an error if passed an array, and that source code comment should be removed.

You may be able to pass in an either/or relation type string for the relation type argument of an association.

I.e. you might be able to get away with

has_many :out, :classes, type: ":STUDENT_OF|:MEMBER_OF", unique: true, model_class: [:Class, :Extracurricular]

I'll also note that the unique option (which corresponds to CREATE UNIQUE) is deprecated in Neo4j.

jorroll avatar Oct 09 '18 22:10 jorroll

It's also indicated in the error message:

ArgumentError: rel_class option must by String, Symbol, false, nil, or an Array of Symbols/Strings

and in this documentation page:

https://www.rubydoc.info/gems/neo4j/Neo4j/ActiveNode/HasN/ClassMethods#has_many-instance_method

But I agree re: seeing no tests/no handling in the source code about being able to accept an Array.

Going to see now if I get away with the relation type string.

anshizzle avatar Oct 09 '18 22:10 anshizzle