#138 ✓resolved
Andreas Ronge

Support for count in has_n

Reported by Andreas Ronge | December 3rd, 2010 @ 10:37 PM | in 1.0.0.beta.24

This is also needed when doing pagination (see will_paginate gem).

currently pagination of a "has_n" relationship will cause
a traversal of every single node in that in order to count the size.

There are two solutions to this
1. Introduce a count property
2. Implement a fast traversal in Java for simply counting by traversal.

Also, should be a size/count property for the #all method.
Person.all.size #=> would be nice if it did not traverse every single node

Comments and changes to this ticket

  • Andreas Ronge

    Andreas Ronge December 6th, 2010 @ 09:12 AM

    • Milestone set to 1.0.0.beta.24
    • Milestone order changed from “22” to “0”
  • Andreas Ronge

    Andreas Ronge December 7th, 2010 @ 08:30 PM

    • State changed from “new” to “open”

    (from [8ea264a99f0238dab521380038c1103aff7f14b2]) Rules can now have function, example sum,max,min,average,count [#138 state:open]


    class Person

     include Neo4j::NodeMixin
     property :age
     rule(:old) { age > 25 }
     rule_obj(RuleSum, :young, :age)  # this works now, just temporary
     # this will be changed to something like this
     # function(:all, Sum.new(:age))


    Person.sum(:all, :age) #=> return the sum of all ages Person.sum(:old, :age) #=> only return the sum of

    Maybe we should also support

    Person.all.sum(:age) Person.old.sum(.age) https://github.com/andreasronge/neo4j/commit/8ea264a99f0238dab52138...

  • Andreas Ronge

    Andreas Ronge December 9th, 2010 @ 09:27 PM

    (from [bfef3a40279e282386b1cabdec776bb3fa5ac0ab]) Massive refactoring of rules to support functions [#138].


    class SumNode

     include Neo4j::NodeMixin
     property :age
     rule(:old, :functions => [Sum.new(:age)]) { age >= 10 }


    SumNode.sum(:old, :age) => sum of the ages of people of age above 10

    Complete example of the Sum Function:

    class Sum < Function
      # Updates the function's value.
      # Called after the transactions commits and a property has been changed on a node.
      # ==== Arguments
      # * rule_name :: the name of the rule group
      # * rule_node :: the node which contains the value of this function
      # * old_value new value :: the changed value of the property (when the transaction commits)
      def update(rule_name, rule_node, old_value, new_value)
        key            = rule_node_property(rule_name)
        rule_node[key] ||= 0
        old_value      ||= 0
        new_value      ||= 0
        rule_node[key] += new_value - old_value
      def self.function_name


    Todo: fix last pending RSpec, "when a node is deleted the function's value should be updated"
    Todo: Implement SumNode.old.sum(:age) == SumNode.sum(:old, :age) ?
    Todo: Documentation and Guides
    Todo: Simplify writing your own rules ?

  • Andreas Ronge

    Andreas Ronge December 15th, 2010 @ 11:33 AM

    • State changed from “open” to “resolved”

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Neo4j.rb is a graph database framework for JRuby.

It provides:

* Mapping of ruby objects to nodes in networks rather than in tables.
* Storage of ruby object to a file system.
* Fast traversal of relationships between nodes in a hugh node space.
* Transaction with rollbacks support.
* Indexing and quering of ruby objects.
* Integration with Rails

People watching this ticket

Referenced by