1

I get a trouble about customize node label in Neo4jrb.

class Core::Product
  include Neo4j::ActiveNode
  id_property :id
  property :name
  property :code
  property :stock_quantity, type: Integer
  property :sale_price, type: Float
  property :last_update, type: DateTime
end

When I create new node, it will has label as Core::Product. I want it to be Product instead.

According this post It seem that _classname property could resolve my problem but I have no idea how to implement it.

Any ideas?

Doom
  • 13
  • 2

1 Answers1

1

Co-maintainer of Neo4j.rb here and author/responsible party for _classname. _classname is a very legacy option at this point, a holdover from when some DB responses didn't include node labels or relationship types. You can override automatic label assignment by calling self.mapped_label_name = in your model.

class Core::Product
  include Neo4j::ActiveNode
  self.mapped_label_name = 'Product'
  # etc,...
end

You'll also want to be aware that the auto-location of association models won't work correctly, so instead of this:

has_many :out, :products, type: 'HAS_PRODUCT'

You'll need to do this:

has_many :out, :products, model_class: 'Core::Product', type: 'HAS_PRODUCT'

We have an open issue, https://github.com/neo4jrb/neo4j/issues/753, that discusses it. I namespace my models to organize code but want my labels to omit them, so I'd love a configuration option that handles this for me.

FOLLOW-UP

I just merged https://github.com/neo4jrb/neo4j/pull/790 into master. It lets you tell the gem to ignore module names when creating labels. I'm going to put it to work in some code this week but if you'd like to test it out, we always love feedback.

subvertallchris
  • 5,282
  • 2
  • 25
  • 43