ruby-on-railsneo4jneo4j.rb

neo4jrb - Save ActiveRel property from node


I have a ActiveNode like this:

class Nodeexample
  include Neo4j::ActiveNode

  property :name

  validates :name, :presence => true

  has_many :in, :nodeexamples, rel_class: :SomeRel

end

and a ActiveRel model like this:

class SomeRel
  include Neo4j::ActiveRel

  property :some_number, type: Float

  from_class :Nodeexample
  to_class :Nodeexample

  type :SOME_REL
  validates :some_number, :presence => true

end

I'm trying to figure out how to add the "some_number" property in the ActiveRel model:

node1 = Nodeexample.new(name:"node1")
node2 = Nodeexample.new(name:"node2")

#how can I add the "some_number" property in the ActiveRel class from here?
node1.nodes << node2

node1.save

The only way I know is by saving the two nodes first and them save the relationship, but i want to save all at once, is it possible? It's strange save 100 nodes and then save 500 relationships :(


Solution

  • You should be able to do this with the create method:

    node1 = Nodeexample.new(name:"node1")
    node2 = Nodeexample.new(name:"node2")
    
    node1.nodes.create(node2, some_number: 123)
    

    There shouldn't be a need for node1.save when using either << or create