scauglog
scauglog

Reputation: 11

neo4j.rb relationship between existing node

I'm new to neo4j (2.3.0) and i want to play with relationship property I use the gem neo4j.rb from andreasronge I create a relationship class

class Connection < Neo4j::Rails::Relationship
  property :connection_type, :machine, :quote => :exact # index both properties
  property :reference, :type => String
  property :updated_at
  property :created_at
end

and model class

class Part < Neo4j::Rails::Model
  property :name, :type => String, :index => :exact
  property :updated_at
  property :created_at
  has_n(:subpart)
  has_n(:connect_to).to(Part).relationship(Connection)
  has_n(:connection_from).from(Part, :connect_to)
  validates_presence_of :name
end

I would like to create a multiple connection between two part with different property for example

part1= Part.new(name:'p1')
part2= Part.new(name:'p2')
part3= Part.new(name:'p3')
c1 = part1.connect_to << part2
c1.type = 'blue'
c2 = part1.connect_to << part2
c2.type = 'red'
c3 = part1.connect_to << part3
c3.type = 'blue'

but c1, c2 and c3 classes are not Connection but Relationship so I can't specify the property type.

Upvotes: 1

Views: 296

Answers (1)

subvertallchris
subvertallchris

Reputation: 5472

I'm assuming you're using Neo4j.rb 2.3, since you call the "relationship" method in has_n(:connect_to).to(Part).relationship(Connection) and that is not going to be included in version 3, currently available in alpha form. If you are using 3.0 alpha, though, you have a problem right there. Some of the documentation on the wiki refers to the 3.0 version and if you do some searches, you'll find lots of documentation for version 1.0 that doesn't apply.

You need to save one of your nodes before your relationship will be persisted. You also won't be able to set properties the way you are trying. Do this instead.

part1 = Part.create(name:'p1')
part2 = Part.create(name:'p2')
part3 = Part.create(name:'p3')

part1.connect_to << part2
part1.save

c1 = part1.rels.to_other(part2).first
c1.name = 'this is a name'
c1[:type] = 'blue'
c1.save

part1.connect_to << part2
part1.save
c2 = part1.rels.to_other(part2).to_a[1]
c2[:type] = 'red'

part1.connect_to << part3
part1.save
c3 = part1.rels.to_other(part3).first
c3[:type] = 'blue'
c3.save

Your properties will be stored. You need to use symbols to create new properties if they aren't already defined on a relationship object. In your case, you'd have been able to access the name method on the relationship because you defined it ahead of time, as demonstrated above.

Upvotes: 2

Related Questions