How to define this owner / member relationship in Neo4j.rb?

I am trying to figure out how to define this owner / member relationship in Neo4j.rb Active :: Node models.

  • Users can create many teams (and take ownership of these teams)
  • Users can receive the commands they have created.
  • Teams have one owner (user) and many members (other users).
  • Owners can add other users as team members.
  • Users can receive all teams that are either owner or member

So far I have something like this, but it doesn't work correctly and I am completely lost.

class User
  include Neo4j::ActiveNode
  has_many :out, :my_teams, model_class: 'Team'
end

class Team
  include Neo4j::ActiveNode
  property :name, type: String
  has_one :in, :owner, model_class: 'User'
end

user = create(:user)
team = build(:team)
user.my_teams << team
expect(team.owner).to eq user

      

+3


source to share


1 answer


First, you have to make sure you are using the 5.0.0 gems that were released yesterday (yay!)

Secondly (and you should get error messages about this when you upgrade), you must provide a parameter type

for your associations, for example:

class User
  include Neo4j::ActiveNode
  has_many :out, :my_teams, type: :OWNS_TEAM, model_class: 'Team'
end

      

This tells ActiveNode

what types of relationships to use for creation and queries.



Finally, to create, you use the class methods for the model classes as follows:

user = User.create
team = Team.create

user.my_teams << team

      

In a somewhat picky personal note, I also suggest the name of the association, teams

or owned_teams

because it creates the methods you use to get these commands from the custom object.

+4


source







All Articles