1

I have this class

class RelatedExternalDe
  include Neo4j::ActiveNode

  property :eid
  property :name
  property :source
  validates :eid, presence: true

  has_many :in, :related_data_elements, type: 'related_external_de',  model_class: RelatedDataElement, unique: true

end

then in the controller I retrieve the nodes. The nodes are retrieved correctly because they are displaying in view correctly.

The controller code is:

@external_data_elements = RelatedExternalDe.search(params[:search])
@external_data_elements.each do |external_de|
   external_de.related_data_elements
end

I get the following error: undefined method `related_data_elements' for # RelatedExternalDe:0x007fefa4fd6b78>

I tried using RelatedExternalDe.find_by instead of running the query and I get the same outcome. Now, if I created an object with new "RelatedExternalDe.new" everything seems to work. Any ideas what I am doing wrong?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189

2 Answers2

0

I'm not sure what's wrong, everything looks correct to me. I have a few thoughts, though:

What version of the neo4j / neo4j-core gems are you using?

Why are you just iterating over the results and calling the association? Is that just demonstration code? Otherwise it seems like it would be pointless.

I'd suggest using a Symbol or String for the model_class like this:

  has_many :in, :related_data_elements, type: 'related_external_de',  model_class: :RelatedDataElement, unique: true

Just to take care of load-order issues.

If none of that helps (and I suspect it won't, unfortunately), we might be able to help you faster in our Gitter chat room: https://gitter.im/neo4jrb/neo4j

Brian Underwood
  • 10,746
  • 1
  • 22
  • 34
  • I am using this: gem 'neo4j', '~> 5.0.0'. yes, it should be external_de.related_data_elements.each do |data_element| etc, but I dont even get to that – user3038783 Oct 07 '15 at 20:25
  • Could you try either doing a `bundle update neo4j` to get the latest in the 5.0.x series or try `'~> 5.2.0'`? – Brian Underwood Oct 07 '15 at 20:27
  • ok, thank you. I thought maybe its because I am using "in" relationship. I changed it to both and I get the same result. Let me try updating – user3038783 Oct 07 '15 at 20:29
  • I had also added `def initialize @related_elements = Array.new end def add_related_element(element) @related_elements << element end def related_elements @related_elements end ` and if in the controller I iterate over the results invoking external_de.add_related_element () I get the same error : undefined add_related_element and I also noticed that initialize is not being invoked, unless I create the object with new, instead of getting it from search result. – user3038783 Oct 07 '15 at 20:51
  • If you add an initialize it will override the ActiveNode initialize. You can use a (before|after)_initialize hook, I believe or you can call super in your initialize to call the one from the gem (I'd suggest the first option) – Brian Underwood Oct 07 '15 at 21:49
0

Also, you could simple have a related_elements method which does '@related_elements ||= Array.new' and then you can call that to get he array and append to it without needing the other method

Brian Underwood
  • 10,746
  • 1
  • 22
  • 34
  • ok, thats good. but there is still the problem that that method will be undefined for the object if the object is return as part of a query. That's what I dont get. it's like the query returns something else and not the actual object of that class, but yet from the error it does seem that the object is of the right class # – user3038783 Oct 07 '15 at 21:57
  • Huh, weird. I figured removing the initialize from the model would have done it. Do you have anything else different on the model? Maybe try commenting out parts to see if something in the model is causing the issue – Brian Underwood Oct 07 '15 at 22:15