Method: ActiveGraph::Node::Query::QueryProxy#branch

Defined in:
lib/active_graph/node/query/query_proxy.rb

#branch { ... } ⇒ QueryProxy

Executes the relation chain specified in the block, while keeping the current scope

Examples:

Load all people that have friends

Person.all.branch { friends }.to_a # => Returns a list of `Person`

Load all people that has old friends

Person.all.branch { friends.where('age > 70') }.to_a # => Returns a list of `Person`

Yields:

  • the block that will be evaluated starting from the current scope

Returns:



204
205
206
207
208
209
210
211
212
# File 'lib/active_graph/node/query/query_proxy.rb', line 204

def branch(&block)
  fail LocalJumpError, 'no block given' if block.nil?
  # `as(identity)` is here to make sure we get the right variable
  # There might be a deeper problem of the variable changing when we
  # traverse an association
  as(identity).instance_eval(&block).query.proxy_as(self.model, identity).tap do |new_query_proxy|
    propagate_context(new_query_proxy)
  end
end