Skip to content

Instantly share code, notes, and snippets.

@subvertallchris
Created June 23, 2015 13:26
Show Gist options
  • Save subvertallchris/d8a3f684bcf08cb7a2f4 to your computer and use it in GitHub Desktop.
Save subvertallchris/d8a3f684bcf08cb7a2f4 to your computer and use it in GitHub Desktop.
class Person
include Neo4j::ActiveNode
property :name, type: String
property :age, type: Integer
has_many :out, :houses, type: 'OWNS_PROPERTY'
has_many :out, :vehicles, type: 'OWNS_VEHICLE'
end
class House
include Neo4j::ActiveNode
property :address, type: String
has_many :in, :people, origin: :houses
end
class Vehicle
include Neo4j::ActiveNode
property :year
has_many :in, :owners, model_class: 'Person', origin: :vehicles
end
# To create a relationship, you can use << to append a new node or = to reset all relationships
person = Person.create(name: 'James', age: 15)
house = House.create(address: '560 Broadway')
person.houses << house
@cheerfulstoic
Copy link

As to adding a second relationship type, I think you were asking about how that would be represented. I think it's best to come up with different association names in that case. Like this:

class Person
  has_many :out, :owned_houses, type: 'OWNS_PROPERTY', model_class: 'House'
  has_many :out, :residences, type: 'LIVES_IN', model_class: 'House'
end

class House
  has_one :in, :owner, model_class: 'Person', type: 'OWNS_PROPERTY'
  has_many :in, :residents, model_class: 'Person', type: 'LIVES_IN'
  # OR...
  has_one :in, :owner, model_class: 'Person', origin: 'owned_houses'
  has_many :in, :residents, model_class: 'Person', origin: 'residences'
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment