Created
September 5, 2014 14:12
-
-
Save subvertallchris/c93dd92f955ecb181241 to your computer and use it in GitHub Desktop.
showing off some of the basics of neo4j.rb 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Student, Lesson, Teacher, Exam].each{|c| c.destroy_all } | |
ss101 = Lesson.create(subject: 'Social Studies', level: 101) | |
ss102 = Lesson.create(subject: 'Social Studies', level: 102) | |
math101 = Lesson.create(subject: 'Math', level: 101) | |
math201 = Lesson.create(subject: 'Math', level: 201) | |
geo103 = Lesson.create(subject: 'Geography', level: 103) | |
sandra = Student.create(name: 'Sandra', age: 16) | |
danny = Student.create(name: 'Danny', age: 15) | |
bobby = Student.create(name: 'Bobby', age: 16) | |
brian = Student.create(name: 'Brian', age: 25) | |
othmar = Teacher.create(name: 'Ms. Othmar') | |
samuels = Teacher.create(name: 'Harold Samuels') | |
ss101_midterm = Exam.create(name: 'SS 101 Midterm') | |
ss101_midterm.lesson = ss101 | |
ss102_midterm = Exam.create(name: 'SS 102 Midterm') | |
ss102_midterm.lesson = ss102 | |
math101_midterm = Exam.create(name: 'Math 101 Midterm') | |
math101_midterm.lesson = math101 | |
math201_midterm = Exam.create(name: 'Math 201 Midterm') | |
math201_midterm.lesson = math201 | |
geo103_midterm = Exam.create(name: 'Geo 103 Midterm') | |
geo103_midterm.lesson = geo103 | |
[sandra, danny].each {|s| StudentLesson.create(from_node: s, to_node: ss101) } | |
[bobby, brian].each {|s| StudentLesson.create(from_node: s, to_node: ss102) } | |
[danny, bobby].each {|s| StudentLesson.create(from_node: s, to_node: math101) } | |
[sandra, brian].each {|s| StudentLesson.create(from_node: s, to_node: math201) } | |
[sandra, danny, bobby, brian].each {|s| StudentLesson.create(from_node: s, to_node: geo103) } | |
[ss101, ss102, geo103].each {|l| l.teachers << othmar } | |
[math101, math201].each {|l| l.teachers << samuels } | |
#sandra's lessons | |
sandra.lessons.each{|l| pp l.full_name } | |
#sandra's classmates | |
sandra.lessons.students.each_with_rel{|student, rel| pp "#{student.name} is in #{rel.from_node.full_name}" } | |
#sandra's classmates, age < 16 | |
sandra.lessons.students(:s).where('s.age < {age}').params(age: 16).each_with_rel{|student, rel| pp "#{student.name} is in #{rel.from_node.full_name}" } | |
@grade = 35.upto(100).to_a | |
def take_exam(exam, student) | |
ExamStudent.create(from_node: exam, to_node: student, grade: @grade.sample) | |
end | |
[sandra, danny].each{|s| take_exam(ss101_midterm, s) } | |
[bobby, brian].each{|s| take_exam(ss102_midterm, s) } | |
[danny, bobby].each{|s| take_exam(math101_midterm, s)} | |
[sandra, brian].each{|s| take_exam(math201_midterm, s)} | |
[sandra, danny, bobby, brian].each{|s| take_exam(geo103_midterm, s) } | |
pp sandra.gpa | |
pp bobby.gpa | |
pp brian.gpa | |
#sandra's lessons where she had a midterm grade greater than 69 | |
sandra.exams(:e, :exam_rel).where('exam_rel.grade > {grade}').lesson(:l).params(grade: 70).pluck(:l, :exam_rel).each{|l, exam_rel| pp "#{l.full_name} with grade #{exam_rel.grade}"} | |
#all students who received grades better than 70 on an exam, returning the students, the exam and its rel, and the lesson | |
Student.as(:s).exams(:e, :exam_rel).where('exam_rel.grade > {grade}').lesson(:l).params(grade: 70).pluck(:s, :e, :exam_rel, :l).each do |student, exam, exam_rel, lesson| | |
pp "#{student.name} (#{student.neo_id}) received a grade of #{exam_rel.grade} on #{exam.name} in lesson #{lesson.full_name}. Current GPA is #{student.gpa}." | |
end | |
# MATCH (s:`Student`), (e:`Exam`), s<-[exam_rel:`taken_by` {_classname: "ExamStudent"}]-(e:`Exam`), (l:`Lesson`), e-[rel2:`given_in`]->(l:`Lesson`) WHERE exam_rel.grade > {grade} RETURN s, e, exam_rel, l | {:grade=>70} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment