How to define these owner/member relationships in Neo4j.rb?

154 views Asked by At

I'm trying to figure out how to define these owner/member relationships in my Neo4j.rb Active::Node models.

  • Users can create many teams (and become the "owner" of those teams)
  • Users can fetch the teams that they have created
  • Teams have a single owner (user) and many members (other users)
  • Owners can add other users as members to a team
  • Users can fetch all teams that they are either an owner or a member of

So far I have something like this, but it isn't working right and I am totally 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
1

There are 1 answers

4
Brian Underwood On

Firstly you should make sure you're using 5.0.0 of the gems which were just released yesterday (yay!)

Secondly (and you should get error messages about this when you upgrade), you should be specifying a type option for your associations, like this:

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

That tells ActiveNode which relationship types to use for creation and querying.

Lastly, for creation you use class methods on the model classes like this:

user = User.create
team = Team.create

user.my_teams << team

On a somewhat picky personal note, I'd also suggest an association name of teams or owned_teams because it creates methods which you use to get those teams from the user object.