Browse files

fixed wrong example using has_n friends_rels

  • Loading branch information...
1 parent e571847 commit 3f66850c75f4d4944983f9448b3bf53e626eeafa @andreasronge committed Feb 26, 2012
Showing with 27 additions and 23 deletions.
  1. +27 −23 source/rails3.textile
@@ -259,69 +259,73 @@ If an validation error occurs the errors will be aggregated on the 'root' object
h4. Finding Relationships
-The has_n accessor method allows you to find relationships.
+The @has_n/has_one@ declaration creates accessors for both nodes and relationships.
+For has_n relationships the name of the accessor is @<name>_rels@ and for has_one it is
+Both accessors for nodes and relationships returns objects that includes the "Ruby Enumerable mixin":
- p1 = Person.create(:name => 'p1')
- p2 = Person.create(:name => 'p2')
- p3 = Person.create(:name => 'p3')
+p1 = Person.create(:name => 'p1')
+p2 = Person.create(:name => 'p2')
+p3 = Person.create(:name => 'p3')
- p1.friends << p2 << p3
+p1.friends << p2 << p3
- # find the relationship object between p1 and p3
- rel = p1.friends.find(p3)
- # set a property on this relationship
- rel[:since] = 1998
+# set the since property for all relationships between p1 and p2, and p1 and p3:
+p1.friends_rels.each{|r| r[:since] = 1998;!}
+p1.friends_rels.first[:since] # => 1998
-The find method also accepts a block (just the normal Ruby Enumerable#find method).
+You can also use the @rels@ method for accessing relationships:
-You can also find relationship using the @friends_rels@ method or the @rels@ method.
- rel = p1.rels(:friends).find{|rel| rel.end_node == p3}
+ rel = p1.rels(Person.friends).find{|rel| rel.end_node == p3}
For more info about the rels method read "here":
-h4. Finding all Relationships between two nodes
+h4. Finding All Relationships Between Two Nodes
The following example returns all relationships between a and b.
+The relationship accessor method also allows you to find the relationships between two nodes using the @to_other@ method (just like the @#rels@ method).
To only find the friends relationships between those nodes:
+# find all relationships object between p1 and p3, using the has_n accessor
-Notice, in neo4j.rb version < 1.7 you have to use the Neo4j::Node api (a._java_node.rels).
+# not using the has_n accessor
h4. destroy and delete
The destroy and delete method works like the Active Record methods.
- rel = p1.rels(:friends).find{|rel| rel.end_node == p3}
- rel.destroy
+rel = p1.rels(:friends).find{|rel| rel.end_node == p3}
- p1.friends.find(p2).delete
You can also delete the relationship object like this.
- p1.friends.delete(p2)
h4. delete_all and destroy_all
You can destroy and delete all relationship in one go (just like Active Record).
- p1.friends.destroy_all
To destroy all relationships:

0 comments on commit 3f66850

Please sign in to comment.