Module: Neo4j::Rails::HasN::ClassMethods

Defined in:
lib/neo4j/rails/has_n.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) has_n(*args) Also known as: has_many

Create a number of methods similar to active record has_many. The first one returns an Relationships::NodesDSL the second generate method (with the _rels postfix) returns a Relationships::RelsDSL

See also Neo4j::NodeMixin#has_n which only work with persisted relationships.

Examples:

class Thing < Neo4j::Rails::Model
  has_n(:things)
end

t = Thing.new
t.things << Thing.new << OtherClass.new
t.save # saves all nodes and relationships

declare a to relationship

class Company
  has_n(:employees).to(Person)
  # alternative - has_n(:employees).to("Person")
end

c = Company.new
c.employees << Person.new << Person.new(:name => 'kalle')

creates a new person and relationship

c.employees.build(:name => 'sune')

creates a new person and relationship and persist it

c.employees.create(:name => 'sune')

delete all nodes and relationships

c.employees.destroy_all

access the relationships and destroy them

c.employees_rels.destroy_all

advanced traversal, using Neo4j::Core::Traversal

c._outgoing(Company.employees).outgoing(:friends).depth.each{ }

declare incoming

class Person < Neo4j::Rails::Model
  has_n(:likes).to(Song)
end

class Song < Neo4j::Rails::Model
  has_n(:people_liking_this_song).from(Person.likes)  # or from(Person, likes)
end

Song.people_liking_this_song.build # creates a Person

See Also:



61
62
63
64
# File 'lib/neo4j/rails/has_n.rb', line 61

def has_n(*args)
  options = args.extract_options!
  define_has_n_methods_for(args.first, options)
end

- (Object) has_one(*args)

Declares ONE incoming or outgoing relationship

Examples:

class Person
  has_one(:friend).to(OtherClass)
  has_one(:root).from("SomeOtherClassAsString")
end
person.best_friend = my_friend
person.best_friend # => my_friend
person.build_best_friend(:name => 'foo')
person.create_best_friend(:name => 'foo')
person.create_best_friend!(:name => 'foo')


93
94
95
96
# File 'lib/neo4j/rails/has_n.rb', line 93

def has_one(*args)
  options = args.extract_options!
  define_has_one_methods_for(args.first, options)
end