Reputation: 6856
Consider this simple setup:
class Person
include Neo4j::ActiveNode
property :name, type: String
has_many :out, :follows, model_class: Person, rel_class: Friendship
has_many :in, :followed_by, model_class: Person, rel_class: Friendship
end
class Friendship
include Neo4j::ActiveRel
property :key, type: String
type 'friendship'
from_class Person
to_class Person
end
How would I search through all Friendship
s for those matching a condition? (e.g. Friendship
s of a certain key).
In an email, Brian Underwood points me to this snippet:
ModelClass.association_name(:node_var, :rel_var).where("rel_var = 'some_condition'")
I've tried playing around with it, but don't understand. Is ModelClass
an ActiveNode
or ActiveRel
instance? What is :node_var
and :rel_var
?
Upvotes: 0
Views: 77
Reputation: 5482
If you want to search for every friendship that has a specific key
property, you'd do that like this:
Person.all.follows.rel_where(key: your_key_var)
# OR
Person.all.follows(:f, :r).where('r.key = {key}').params(key: your_key_var)
These will both generate MATCH (p:Person)-[r:friends]->(f:Person)
, more or less, with the first example using auto-defined node identifiers and the second using f
for the destination Friend node and r
for the relationship, as given by the :f, :r
arguments. After that, a to_a
will return the friend at the END of the chain or you can call pluck
with either :f
or :r
to return the given objects.
The model_class
option always describes the NODE class on the other side of the association. In Brian's example, node_var
and rel_var
are generic names for the identifiers Cypher will use in the statement that it creates.
Upvotes: 2