Skip to content

Instantly share code, notes, and snippets.

@jmdeldin
Created September 2, 2015 21:54
Show Gist options
  • Save jmdeldin/2d67e63e84021426d669 to your computer and use it in GitHub Desktop.
Save jmdeldin/2d67e63e84021426d669 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
property :enjoyed_writing, type: Boolean, default: true
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')
BookAuthor.create!(from_node: a1, to_node: b1)
BookAuthor.create!(from_node: a1, to_node: b2, enjoyed_writing: false)
BookAuthor.create!(from_node: a2, to_node: b2) # co-authors
# find the book that a1 didn't enjoy writing
# 1. this works
rs = a1.books.rel_where(enjoyed_writing: false).to_a
puts "books: #{rs.length} (should be 1)"
# 2. this does not cast 'false' into false
rs = a1.books.rel_where(enjoyed_writing: 'false').to_a
puts "books: #{rs.length} (should be 1)"
# 3. it's necessary to reload to use rel_where
a1.books.to_a
rs = a1.books.rel_where(enjoyed_writing: false).to_a
puts "books: #{rs.length} (should *not* be 2)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment