SoWhat
SoWhat

Reputation: 5622

Using neo4jRb Gem, how do I get the relationship on a has_one association

So I created a has_one and has_many relationship with neo4j

class Client

    has_one  :out , :room,  model_class: :Room,rel_class: :AssignedTo

end

class Room
  has_many  :in , :clients, rel_class: :AssignedTo, model_class: :Client

end

class AssignedTo
    include Neo4j::ActiveRel
    from_class :Client
    to_class :Room
    type 'assigned_to'
    property :from_date, type: DateTime
    property :to_date , type: DateTime
end

I want to accept the Assigned_to relationship from room to client room.clients.each_with_rel works fine but I can't find a way to access the relationship the other way around: client.room.rel All the methods i tried client.room.rel,relationship, assigned_to etc don't seem to work

Upvotes: 0

Views: 120

Answers (1)

John
John

Reputation: 10079

Since client.room is a has_one relationship, by default neo4jrb gets the association proxy, and then plucks the first (and only) result, returning the room object. The room ActiveNode object doesn't have an .each_with_rel method.

Using the latest version of the Neo4jrb gem (dunno which version you are using) you can do client.room(chainable: true).each_with_rel do |node, rel| which should work, same as room.clients.each_with_rel do |node, rel|.

The chainable: true option for has_one associations tells neo4jrb to return an association proxy (which is what you always get with a has_many association).

Upvotes: 1

Related Questions