Skip to content

Instantly share code, notes, and snippets.

@jmdeldin
Last active August 29, 2015 14:26
Show Gist options
  • Save jmdeldin/ca6367bb93580520d08b to your computer and use it in GitHub Desktop.
Save jmdeldin/ca6367bb93580520d08b to your computer and use it in GitHub Desktop.
class Book
include Neo4j::ActiveNode
property :title
has_many :in, :spaces, rel_class: :BookAuthor
end
class Author
include Neo4j::ActiveNode
property :first_name
property :last_name
has_many :out, :books, rel_class: :BookAuthor
end
class BookAuthor
include Neo4j::ActiveRel
from_class :Author
to_class :Book
type 'book_author'
creates_unique_rel
end
# clear out the db
Neo4j::Session.query('START n=node(*) MATCH (n)-[r]->(m) DELETE n,m,r').to_a
a1 = Author.create!(first_name: 'Doubting', last_name: 'Thomas')
a2 = Author.create!(first_name: 'Debbie', last_name: 'Downer')
b1 = Book.create!(title: 'How to lose friends and annoy people')
b2 = Book.create!(title: 'The Art of Foo Bar')
# co-authors...
[a1, a2].each do |a|
[b1, b2].each do |b|
rel = BookAuthor.new
rel.from_node = a
rel.to_node = b
rel.save!
end
end
# a1 is no longer a co-author of b1
count = -> {
Neo4j::Session.query.
match('(a)-[r:book_author]->(b)').
where("ID(a) = ?", a1.neo_id).
return(:b).
to_a.length
}
puts "pre-delete: #{count.()}"
del = Neo4j::Session.query.
match('(a)-[r:book_author]->(b)').
where("ID(a) = ? AND ID(b) = ?", a1.neo_id, b1.neo_id).
delete(:r).
exec
puts "parameterized: #{count.()}"
del = Neo4j::Session.query.
match('(a)-[r:book_author]->(b)').
where("ID(a) = #{a1.neo_id} AND ID(b) = #{b1.neo_id}").
delete(:r).
exec
puts "interpolated: #{count.()}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment