0

I got two types of relationships between two classes. In one of them, I need to establish the origin, however I don't know how. Anyone idea?

class A
  include Neo4j::ActiveNode
  ...
  has_many :out, :method1, model_class: B
  has_many :out, :method2, model_class: B
  ...
end

class B
  include Neo4j::ActiveNode
  ...
  has_one :in, :something, model_class: A, origin: ?????
  ...
end

Graph

The type of the relationship is the default one ("#"+ name of the method).

http://i.imgur.com/3MtC0k3.png

tehAnswer
  • 960
  • 1
  • 13
  • 28

2 Answers2

2

If you look at the documentation on declaring origins I think the issue you're having is that you haven't declared a second parameter that sets the method.

So in your class A, you might want to do this:

has_many :out, :somethingHere :type1, model_class: B

Then in B, you might want to do this:

has_one :in, :something, model_class: A, :origin: :somethingHere

I can't tell you what somethingHere should be because you haven't provided enough information about what A and B are to tell what their association would mean.

FrobberOfBits
  • 17,634
  • 4
  • 52
  • 86
  • 1
    This is the answer. The `origin` options makes the association look for another association of the same name on the corresponding model. You'd use `has_one :in, :something, model_class: A, origin: :method1` and then `B.first.something` would use `#method1` as its relationship type. – subvertallchris Dec 24 '14 at 17:55
  • The thing is if you put `origin: :method1`, everyone B with the incoming `#method2` relationship will return `nil`. Do you feel me? – tehAnswer Dec 24 '14 at 21:08
0

I ended up making my own method. It's something like:

class A
  include Neo4j::ActiveNode
  ...
  has_many :out, :method1, model_class: B
  has_many :out, :method2, model_class: B
  ...
end

class B
  include Neo4j::ActiveNode
  ...
  # I removed has_one line and include:
  def something
    rel = rels(dir: :incoming).first
    return nil if rel.nil?
    rel.start_node
  end
  ...
end

Is it a good solution? Comments please.

tehAnswer
  • 960
  • 1
  • 13
  • 28
  • If it works, it works. Not going to argue against it, but it does seem odd for you to define a custom method like this when the framework provides hooks to define such methods for you. – FrobberOfBits Dec 24 '14 at 18:06
  • Yeah, I don't understand why this is necessary. See my comment on FrobberOfBits's answer. You can email me if you need extra help, I'm one of the maintainers of the Neo4j gem. – subvertallchris Dec 24 '14 at 20:44