Method: Valkyrie::Persistence::Postgres::QueryService#find_inverse_references_by

Defined in:
lib/valkyrie/persistence/postgres/query_service.rb

#find_inverse_references_by(resource: nil, id: nil, property:, model: nil) ⇒ Array<Valkyrie::Resource>

Get all resources which link to a resource with a given property.

Parameters:

  • resource (Valkyrie::Resource) (defaults to: nil)

    The resource which is being referenced by other resources. Requires either resource or id parameter to be specified.

  • id (Valkyrie::ID) (defaults to: nil)

    ID of the resource which is being reference by other resources. Requires either resource or id parameter to be specified.

  • property (Symbol)

    The property which, on other resources, is referencing the given ‘resource`

  • model (Class) (defaults to: nil)

    Filter results to include only instances of this model. (optional)

Returns:

  • (Array<Valkyrie::Resource>)

    All resources in the persistence backend which have the ID of the given ‘resource` in their `property` property. Not in order.

Raises:

  • (ArgumentError)

    Raised when the ID is not in the persistence backend.



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/valkyrie/persistence/postgres/query_service.rb', line 116

def find_inverse_references_by(resource: nil, id: nil, property:, model: nil)
  raise ArgumentError, "Provide resource or id" unless resource || id
  ensure_persisted(resource) if resource
  id ||= resource.id
  internal_array = "{\"#{property}\": [{\"id\": \"#{id}\"}]}"
  if model
    run_query(find_inverse_references_with_type_query, internal_array, model)
  else
    run_query(find_inverse_references_query, internal_array)
  end
end