Using rules from related model

19 views
Skip to first unread message

Cyprian Kowalczyk

unread,
Apr 3, 2012, 10:06:03 AM4/3/12
to neo...@googlegroups.com
Hi all,

Say we have a User that has many Addresses and only one address can be active (we have a rule(:active) on Address).
Can I currently do this `User.first.addresses.active` to get the active address?

kind regards,
Cyprian 

Andreas Ronge

unread,
Apr 3, 2012, 11:57:12 AM4/3/12
to neo...@googlegroups.com
Hi

Hmm, that would be nice.
We can't have a rule on the User which return an Address (I think).
But we can have a rule on the User class which return users which have
an active address (which is not what you want).

Maybe we could support it with something like this:
class User
has_n :addresses
rule(:active, :addresses) { |address| address.active? }
end

User.first.addresses.active #=> only active addresses

Will do some thinking ...
Cheers
Andreas

> --
> You received this message because you are subscribed to the Google Groups
> "neo4jrb" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/neo4jrb/-/d8-4d6j7j9UJ.
> To post to this group, send email to neo...@googlegroups.com.
> To unsubscribe from this group, send email to
> neo4jrb+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/neo4jrb?hl=en.

Cyprian Kowalczyk

unread,
Apr 5, 2012, 2:47:53 AM4/5/12
to neo...@googlegroups.com
Hi Andres,

Such chaining would be possible if User.first.addresses would return Neo4j::Traversal::Traverser or if the return classes were otherwise standardized allowing method chaining for queries. Imagine all the flexibility it would allow. The construct should also allow execute a query on such chained calls.
Duplicating rules as you proposed in User is not really DRY.


kind regards,
Cyprian



On Tuesday, April 3, 2012 5:57:12 PM UTC+2, ronge wrote:
Hi

Hmm, that would be nice.
We can't have a rule on the User which return an Address (I think).
But we can have a rule on the User class which return users which have
an active address (which is not what you want).

Maybe we could support it with something like this:
  class User
    has_n :addresses
    rule(:active, :addresses) { |address| address.active? }
  end

  User.first.addresses.active #=> only active addresses

Will do some thinking ...
Cheers
Andreas

Cyprian Kowalczyk

unread,
Apr 5, 2012, 2:49:27 AM4/5/12
to neo...@googlegroups.com
Andreas, sorry for the typo in your name!

Andreas Ronge

unread,
Apr 6, 2012, 10:26:09 AM4/6/12
to neo...@googlegroups.com
Hi

User.first.addresses does already return a Traversal.
If the User and Address class were declared like this:
class Address < Neo4j::Rails::Model
rule(:active) { self[:active] == true}
end
class User < Neo4j::Rails::Model
has_n(:addresses).to(Address)
end
Then it would possible to (automatically) add the method "active" on
the Traversal returned by User.first.addresses which will check if the
Address nodes has an incoming relationship '_active' to the class node
(rule node).

Another alternative is top allow combining Rules/Travesal and Cypher queries.
Example:
User.all.addesses.query{|address| address.city == 'malmoe'}
Just some quick thoughts ...

Cheers
Andreas

> --
> You received this message because you are subscribed to the Google Groups
> "neo4jrb" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/neo4jrb/-/oQTlZJw16JAJ.

Cyprian Kowalczyk

unread,
Apr 9, 2012, 5:39:19 AM4/9/12
to neo...@googlegroups.com
Hi Andreas,

Isn't it that User.first.addresses returns `Neo4j::Rails::Relationships::NodesDSL`?
User.first.addresses.all woks, User.first.addresses.active does not.

kind regards,
Cyprian



On Friday, April 6, 2012 4:26:09 PM UTC+2, ronge wrote:
Hi

User.first.addresses does already return a Traversal.


If the User and Address class were declared like this:
  class Address < Neo4j::Rails::Model
     rule(:active) { self[:active] == true}
  end
  class User < Neo4j::Rails::Model
    has_n(:addresses).to(Address)
  end
Then it would possible to (automatically) add the method "active" on
the Traversal returned by User.first.addresses which will check if the
Address nodes has an incoming relationship '_active' to the class node
(rule node).

Another alternative is top allow combining Rules/Travesal and Cypher queries.
Example:
  User.all.addesses.query{|address| address.city == 'malmoe'}
Just some quick thoughts ...

Cheers
Andreas

Andreas Ronge

unread,
Apr 16, 2012, 6:30:44 AM4/16/12
to neo...@googlegroups.com
Hi

I've now started to look at this, it's probably not that hard to
implement (or at least some of it)
I've created a new issue https://github.com/andreasronge/neo4j/issues/181

Cheers
Andreas

>> > neo4jrb+u...@googlegroups.com.


>> > For more options, visit this group at
>> > http://groups.google.com/group/neo4jrb?hl=en.
>

> --
> You received this message because you are subscribed to the Google Groups
> "neo4jrb" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/neo4jrb/-/ifvHCW3pXbMJ.


>
> To post to this group, send email to neo...@googlegroups.com.
> To unsubscribe from this group, send email to

> neo4jrb+u...@googlegroups.com.

Cyprian Kowalczyk

unread,
Apr 16, 2012, 7:01:35 AM4/16/12
to neo...@googlegroups.com
Great! I'm looking forward to test it!

kind regards,
Cyprian


On 16 kwi 2012, at 12:30, Andreas Ronge wrote:

> Hi
>
> I've now started to look at this, it's probably not that hard to
> implement (or at least some of it)
> I've created a new issue https://github.com/andreasronge/neo4j/issues/181
>
> Cheers
> Andreas
>

Andreas Ronge

unread,
Apr 17, 2012, 4:25:44 PM4/17/12
to neo...@googlegroups.com
Hi

I'm making good progress on combining cypher, the traversal api and rules.
Check these RSpecs -
https://github.com/andreasronge/neo4j-wrapper/blob/master/spec/neo4j/rule/rule_cypher_integration_spec.rb

My favorite RSpec is this: "Give me all rooms in a dungeon where there
are dangerous monsters"
dungeon.monsters.dangerous { |m| rooms = m.incoming(Room.monsters); rooms }
This will create a cypher query for that (append a #to_s and you get
the cypher query as a String)

For simpler queries a hash value is supported, for example give me all
monsters with a weapon 'sword'
Monster.all(:weapon => 'sword')

Dangerous monsters are defined by a rule:

class Monster
include Neo4j::NodeMixin
property :age
rule :all
rule(:dangerous) { |m| m[:strength] > 15 }
end

Next - fix so this works from Neo4j::Rails::Model as well.

Btw, I removed the Neo4j::NodeIndex and merged into Neo4j::Node, same
with Neo4j::RelationshipIndex

Cheers
Andreas

Reply all
Reply to author
Forward
0 new messages